Kuldeep Bora
Kuldeep Bora

Reputation: 4882

Puppeteer creates bad pdf

I am using puppeteer to create a pdf from my static local html file. The PDF is created but it's corrupted. Adobe reader can't open the file and says - 'Bad file handle'. any suggestions?

I am using below standard code:

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('local_html_file', {waitUntil: 'networkidle2'});
  await page.pdf({path: 'hn.pdf', format: 'A4'});

  await browser.close();
})();

I also tried setContent() but same result. The page.screenshot() function works however.

Upvotes: 1

Views: 2906

Answers (2)

Kuldeep Bora
Kuldeep Bora

Reputation: 4882

The issue was the pdf filename I gave - 'con.pdf' This seems to be a reserved name in windows and hence bad file handle. :D What a coincidence !!! Thanks everyone.

Upvotes: 0

Kadir
Kadir

Reputation: 96

Probably your code triggers exception. You should check pdf file size is not "zero" and you can read your pdf file with less or cat command. Sometimes pdf creators software can write errors to top of the pdf file content.

const puppeteer = require('puppeteer');

(async () => {
  try{
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('local_html_file', {waitUntil: 'networkidle2'});
  await page.pdf({path: 'hn.pdf', format: 'A4'});

  await browser.close(); 
  }catch(e){
   console.log(e);
  }
})();

Upvotes: 2

Related Questions