I used Puppeteer to generate a buffer with pdf information and send it to frontend. Here is the backend api:
exports.getPDF = async (req, res) => {
const { content } = req.query;
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setContent(`
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>HTML to PDF Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
${content}
</body>
</html>
`);
const buffer = await page.pdf();
await browser.close();
return res.apiResponse(buffer);
};
Then I tried to convert the buffer to pdf file and open it but failed, It said failed to load PDF document.
What I've done on front end:
await dispatch({
type: 'resume/fetchResumePDF',
payload: {
content: resumeContent
}
});
console.log(this.props.resumePDF);
const file = new Blob(this.props.resumePDF.data, {
type: 'application/pdf'
});
const getFile = file && window.URL.createObjectURL(file);
window.open(getFile, "_blank");
The console.log
buffer is as below: