Generating PDF from a Phantom Web Page

0

I'm developing in Node an application where I have an html page, which I fill in with bank data and then I need to save this page in PDF. I'm trying to use Phatom, I've tried following several tutorials, but when I pass the page URL it saves the PDF several times, it does not finish the process and always overwrites the generated PDF. What can I do to resolve this?

const timeout = ms => new Promise(resolve => setTimeout(resolve, ms));

(async function() {
    const instance = await phantom.create();
    const page = await instance.createPage();

    await page.property('viewportSize', { width: 1920, height: 1024 });

    const status = await page.open('http://localhost/gerar_diploma?id_aluno='+_id);

    console.time('wait');

    await timeout(500);

    console.timeEnd('wait');

    await page.render('docs/result.pdf');

    await instance.exit();
})();
    
asked by anonymous 05.12.2018 / 18:01

1 answer

0

If you have difficulty here is the method, if you just want to export the method createPDF and import and pass the url by parameter.

const phantom = require('phantom');

async function createPDF(url) {
  const instance = await phantom.create();
  const page = await instance.createPage();

  await page.property('viewportSize', { width: 1600, height: 900 });
  const status = await page.open(url);
  console.log('Page opened with status [${status}].');
  await page.render(new Date().getTime()+'-'+'newPDF.pdf');
  await instance.exit();
};


createPDF('https://pt.stackoverflow.com/questions/348094/gerando-pdf-de-uma-p%C3%A1gina-da-web-com-o-phantom')
    
05.12.2018 / 19:38