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
Answer from Piotr Cierpich on Stack Overflow
» npm install html2canvas
» npm install html2canvas-pro
html - A similar solution to html2canvas with nodejs - Stack Overflow
How to use NPM package as normal javascript in HTML - Stack Overflow
How to get started with html2canvas
Is there an "html-2-canvas" that works with modern Angular? (Need to capture and save DOM as an image.)
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
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) return console.log(err);
console.log(res); // { filename: '/app/businesscard.pdf' }
});
» npm install @xuannghia/html2canvas
» npm install @types/html2canvas
Package installed by npm is located in /node_modules/ which cannot be used by front end directly. To use those modules, you need to use ES6 syntax and import them by using the following code:
// Just an example, you need to read the doc to see how to import
import html2cavas from "html2cavas"
However, browser cannot read ES6 syntax directly. As a result, you need to use Babel to transpile your ES6 code to normal JS code. See https://hackernoon.com/use-es6-javascript-syntax-require-import-etc-in-your-front-end-project-5eefcef745c2
After you transpiled your code, use the <script> tag to import the transpiled code.
Alternatively, you might be able to copy what you need from /node_modules/ and copy it to your js folder. However it is not recommended.
You can use the CDN for such library. In this case, check here: https://cdnjs.com/libraries/html2canvas
You should find one for production and regular development there. (https://cdnjs.cloudflare.com/ajax/libs/html2canvas/0.4.1/html2canvas.js) =>development
<script>
const htmlCanvas = require("htmlcanvas")
</script>
» npm install html2canvas-proxy
» npm install jspdf-html2canvas
» npm install vue-html2canvas