-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3af0818
commit 767b657
Showing
8 changed files
with
250 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import PdfPrinter from "pdfmake"; | ||
import btoa from "btoa"; | ||
import fetch from "node-fetch"; | ||
import { extname } from "path"; | ||
|
||
|
||
const fonts = { | ||
Helvetica: { | ||
normal: "Helvetica", | ||
bold: "Helvetica-Bold", | ||
italics: "Helvetica-Oblique", | ||
bolditalics: "Helvetica-BoldOblique", | ||
}, | ||
}; | ||
|
||
const printer = new PdfPrinter(fonts); | ||
|
||
const fetchIamgeBuffer = async (image) => { | ||
let result = await fetch(image, { | ||
responseType: "arraybuffer", | ||
}); | ||
return result.arrayBuffer(); | ||
}; | ||
|
||
export const getPDFReadableStream = async (data) => { | ||
let imagePath = {}; | ||
if (data.image) { | ||
let imageBufferArray = await fetchIamgeBuffer(data.image); | ||
console.log(imageBufferArray); | ||
|
||
const base64String = btoa( | ||
String.fromCharCode(...new Uint8Array(imageBufferArray)) | ||
); | ||
console.log(base64String); | ||
|
||
const imageUrlPath = data.image.split("/"); | ||
const fileName = imageUrlPath[imageUrlPath.length - 1]; | ||
const extension = extname(fileName); | ||
const base64Pdf = `data:image/${extension};base64,${base64String}`; | ||
|
||
imagePath = { image: base64Pdf, width: 500, margin: [0, 0, 0, 40] }; | ||
} | ||
|
||
const docDefinition = { | ||
content: [ | ||
imagePath, | ||
{ text: data.name, fontSize: 20, bold: true, margin: [0, 0, 0, 40] }, | ||
{ text: data.surname, fontSize: 20, bold: true, margin: [0, 0, 0, 40] }, | ||
{ text: data.email, fontSize: 20, bold: true, margin: [0, 0, 0, 40] }, | ||
{ text: data.userName, fontSize: 20, bold: true, margin: [0, 0, 0, 40] }, | ||
{ text: data.job, fontSize: 20, bold: true, margin: [0, 0, 0, 40] }, | ||
{ text: data.bio, fontSize: 20, bold: true, margin: [0, 0, 0, 40] }, | ||
{ text: data.area, fontSize: 20, bold: true, margin: [0, 0, 0, 40] }, | ||
], | ||
defaultStyle: { | ||
font: "Helvetica", | ||
}, | ||
}; | ||
|
||
const options = {}; | ||
|
||
const pdfReadableStream = printer.createPdfKitDocument( | ||
docDefinition, | ||
options | ||
); | ||
// pdfReadableStream.pipe(fs.createWriteStream('document.pdf')); // old syntax for piping | ||
// pipeline(pdfReadableStream, fs.createWriteStream('document.pdf')) // new syntax for piping (we don't want to pipe pdf into file on disk right now) | ||
pdfReadableStream.end(); | ||
return pdfReadableStream; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.