Cannot render pdf with images stored locally
See original GitHub issueI cannot get images stored locally to be rendered in generated pdf with Puppeteer, but external images for which I specify a url work.
In particular, in the sample code below, rendering the page in test_html1 works, while rendering the test_html2 does not work.
(async () => {
const browser = await puppeteer.launch({ args: ['--no-sandbox'] });
const page = await browser.newPage();
const test_html1 = `<html><h3>Hello world!</h3><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/2/2f/Google_2015_logo.svg/1024px-Google_2015_logo.svg.png"></html>`;
// const test_html2 = `<html><h3>Hello world!</h3><img src="file:///home/cristina/Documents/logo.jpg"></html>`;
await page.goto(`data:text/html,${test_html}`, { waitUntil: 'networkidle0' });
await page.pdf({ path: `${this.outputPath}/test-puppeteer.pdf`,
format: 'A4', landscape: !data.isPortrait,
margin: { top: '0.5cm', right: '1cm', bottom: '0.8cm', left: '1cm' }, printBackground: true });
await browser.close();
})();`
Result with test_html1:
Result with test_html2:
Issue Analytics
- State:
- Created 6 years ago
- Reactions:4
- Comments:13 (1 by maintainers)
Top Results From Across the Web
Puppeteer: cannot render pdf with images stored locally
Solved on Puppeteer GitHub: https://github.com/GoogleChrome/puppeteer/issues/1643. Basically, it doesn't work because, as a security measure ...
Read more >Troubleshoot viewing PDF files on the web - Adobe Support
In Reader or Acrobat, right-click the document window, and choose Page Display Preferences. · From the list at left, select Internet. · Deselect ......
Read more >Chrome PDF Viewer Not Working? Here's How to Fix It
How to Fix a PDF That Doesn't Render Properly · Open the Chrome menu by clicking the three dots in the top-right corner....
Read more >Tips for Viewing and Using Fillable PDFs - CT.gov
On the Reader menu, go to File > Save As; Choose either the PDF (recommended) or Text format; You should then be prompted...
Read more >HP Printers - Cannot Print PDFs from Adobe Reader (Windows)
Move the PDF file to your computer's hard drive, and then print again. Adobe recommends storing PDF files on a local hard disk...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve stumbled with this issue too, another solution would be to use data streams for the img src, eg:
function base64_encode(file) { var bitmap = fs.readFileSync(file); return new Buffer(bitmap).toString(‘base64’); } img.src = ‘data:image/png;base64,’ + base64_encode(imagePath);
@acgrama In that case, why don’t you just serve those static files? Something like this should work:
Also I checked Chromium options, but I don’t find useful one for that.