Skip to content Skip to sidebar Skip to footer

A Similar Solution To Html2canvas With Nodejs

I'm using css, jquery and nodejs (with ejs and express) to create a website and I need to export a div with some text and some divs with a background-image as an image (jpg or png)

Solution 1:

I know this question is old, but these days I'd try puppeteer

const puppeteer = require("puppeteer");

async function printPDF() {
  const browser = await puppeteer.launch({ headless: true });
  const page = await browser.newPage();
  await page.goto("http:///stackoverflow.com", {
    waitUntil: "networkidle0",
  });
  const pdf = await page.pdf({
    width: 1200,
    height: 1920,
    pageRanges: "1-2",
    path: "so.pdf",
  });

  await browser.close();
  return pdf;
}
printPDF();

Also see HTML to PDF with Node.js

Solution 2:

Try html-pdf, it export css in pdf file and it is also a npm package so it will compatible with nodejs.

Installation:

$ npm install -g html-pdf

Command-line example:

$ html-pdf test/businesscard.html businesscard.pdf

Code example:

var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./test/businesscard.html', 'utf8');
var options = { format: 'Letter' };

pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
  if (err) returnconsole.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' } 
});

Post a Comment for "A Similar Solution To Html2canvas With Nodejs"