How to print to PDF using Node JS / Webdriver.io / Chimp?

╄→гoц情女王★ 提交于 2019-12-13 02:59:39

问题


I'm trying to use Chimp / Webdriver.io / Selenium in Node JS to try to test my @media print CSS to make sure everything shows up correctly when people print from my website.

How can I programatically get Chrome / Firefox to print to PDF? I don't want to convert a screenshot to PDF. I want the PDF to look like what it will look like when printed.

Then, how can I scan the PDF to make sure that the results are correct?


回答1:


Success! I had to install/use the following tools:

npm install html-pdf-chrome --save-dev
npm install pdfreader --save-dev

html-pdf-chrome is used to magically call Chrome to convert some given HTML to a PDF in the manner that Chrome would normally use to print. pdfreader is a package that reads said PDF and then provides the text inside of it.

After browsing to the page I want to print using webdriver, I can call:

this.When(/^I print the page to a PDF named "([^"]*)"$/,
  async function(outputFilename) {

    console.log("Getting the html...");
    let sourceHTML = await browser.getSource();

    console.log("Printing the html using Chrome...");
    let pdf = await HtmlPdf.create(sourceHTML);

    console.log("Saving the PDF to " + outputFilename + "...");
    await pdf.toFile(path.join(DEFAULT_PRINT_PATH, outputFilename));
  });

Then, to get the text in the PDF, I call this function:

function readPdfText(filename) {
  return new Promise((resolve, reject) => {
    let pdfText = "";
    new pdfReader.PdfReader().parseFileItems(path.join(DEFAULT_PRINT_PATH, filename), function(err, item){
      if (err){
        console.log("Error received on parsing PDF: " + err, err.stack);
        reject(err);
      }
      else if (!item) {
        resolve(pdfText);
      }
      else if (item.text) {
        if(item.text.trim() === ":") {
          pdfText += item.text;
        } else {
          pdfText += "\n" + item.text;
        }
      }
    });
  });
}


来源:https://stackoverflow.com/questions/50166936/how-to-print-to-pdf-using-node-js-webdriver-io-chimp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!