1.2.3 • Published 2 years ago
@hyfi06/html2pdf v1.2.3
@hyfi06/html2pdf
Create pdf from html string. Express middleware.
Install
npm install --save @hyfi06/html2pdf
Examples
const express = require("express");
const app = express();
const pdf = require("../index");
app.use(pdf);
app.use("/pdf", async function (req, res) {
try {
await res.html2pdf({
filename: "example.pdf",
htmlString: "<html><body>example</body></html>",
});
} catch (err) {
console.log(err);
}
});
app.listen(3000, function () {
console.log("Listening http://localhost:3000");
});
Using express router:
const express = require("express");
const pdf = require("../index");
function pdfApi(app) {
const router = express.Router();
app.use("/pdf");
router.get("/", pdf, async function (req, res) {
try {
await res.html2pdf({
filename: "example.pdf",
htmlString: "<html><body>example</body></html>",
});
} catch (err) {
console.log(err);
}
});
}
module.exports = pdfApi;
Options and args
res.html2pdf({
filename: '',
htmlString: '',
[options],
[launchArgs]
});
options
<Object> Options object which might have the following properties:scale
<number> Scale of the webpage rendering. Defaults to1
. Scale amount must be between 0.1 and 2.displayHeaderFooter
<boolean> Display header and footer. Defaults tofalse
.headerTemplate
<string> HTML template for the print header. Should be valid HTML markup with following classes used to inject printing values into them:date
formatted print datetitle
document titleurl
document locationpageNumber
current page numbertotalPages
total pages in the document
footerTemplate
<string> HTML template for the print footer. Should use the same format as theheaderTemplate
.printBackground
<boolean> Print background graphics. Defaults tofalse
.landscape
<boolean> Paper orientation. Defaults tofalse
.pageRanges
<string> Paper ranges to print, e.g., '1-5, 8, 11-13'. Defaults to the empty string, which means print all pages.format
<string> Paper format. If set, takes priority overwidth
orheight
options. Defaults to 'Letter'.width
<string|number> Paper width, accepts values labeled with units.height
<string|number> Paper height, accepts values labeled with units.margin
<Object> Paper margins, defaults to none.preferCSSPageSize
<boolean> Give any CSS@page
size declared in the page priority over what is declared inwidth
andheight
orformat
options. Defaults tofalse
, which will scale the content to fit the paper size.
launchArgs
<Array<string> Additional arguments to pass to the browser instance. The list of Chromium flags can be found here, and here is the list of Firefox flags.
License
Licensed under the MIT License.