html-to-pdf-converter
Version:
HTML to PDF converter with support for HEADERS, FOOTERS and page numbers
55 lines • 2.34 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("./types");
const index_1 = require("./index");
exports.getBodySize = (page) => __awaiter(this, void 0, void 0, function* () {
const body = yield page.$('html');
if (!body) {
throw new Error('Page have to have <body>!');
}
const sizeInPixels = yield body.boundingBox();
if (!sizeInPixels) {
throw new Error('Page have to have <body>!');
}
return {
width: index_1.Pixels.of(sizeInPixels.width).toMillimeters('ceil'),
height: index_1.Pixels.of(sizeInPixels.height).toMillimeters('ceil'),
};
});
exports.mkSizedPdf = (html, page, size, margin = types_1.PrintMargin.ofZero()) => __awaiter(this, void 0, void 0, function* () {
yield page.setContent(html);
const pdfBuffer = yield page.pdf({
width: size.width.toString(),
height: size.height.toString(),
printBackground: true,
displayHeaderFooter: false,
margin: {
top: margin.top.toString(),
right: margin.right.toString(),
bottom: margin.bottom.toString(),
left: margin.left.toString(),
},
});
return pdfBuffer;
});
exports.calcContentSize = (html, pageSize, margin, page) => __awaiter(this, void 0, void 0, function* () {
const contentViewPort = {
width: Math.floor(pageSize.width
.subtract(margin.left)
.subtract(margin.right)
.toPixels()._n),
height: Math.floor(pageSize.height.toPixels()._n),
};
yield page.setViewport(contentViewPort);
yield page.setContent(html);
return exports.getBodySize(page);
});
//# sourceMappingURL=puppeteer.js.map