Skip to content

Instantly share code, notes, and snippets.

@allaniftrue
Created April 11, 2021 14:17
Show Gist options
  • Save allaniftrue/36122e4273770d21cf07ed08b69a33f3 to your computer and use it in GitHub Desktop.
Save allaniftrue/36122e4273770d21cf07ed08b69a33f3 to your computer and use it in GitHub Desktop.
Dompdf API response
const b64ToBlob = (b64Data, contentType = '', sliceSize = 512) => {
const byteCharacters = atob(b64Data);
const byteArrays = [];
for (let offset = 0; offset < byteCharacters.length; offset += sliceSize) {
const slice = byteCharacters.slice(offset, offset + sliceSize);
const byteNumbers = new Array(slice.length);
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i);
}
const byteArray = new Uint8Array(byteNumbers);
byteArrays.push(byteArray);
}
const blob = new Blob(byteArrays, { type: contentType });
return blob;
}
handleDownload = async (row, event) => {
await axios.get(`feature/${feature.id}`/pdf)
.then(response => {
const blob = b64ToBlob(response.data.pdf, 'application/pdf')
const blobUrl = URL.createObjectURL(blob);
window.open(blobUrl)
})
}
<?php
public function pdf($id)
{
//...
$pdf = PDF::loadView('pdf.payroll');
return response()->json([
'pdf' => base64_encode($pdf->output())
]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment