html-to-pdf-converter
Version:
HTML to PDF converter with support for HEADERS, FOOTERS and page numbers
110 lines • 4.76 kB
JavaScript
"use strict";
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 fs_1 = require("fs");
const path_1 = require("path");
const types_1 = require("../src/types");
const index_1 = require("./index");
const test_helpers_1 = require("./utils/test-helpers");
const headerHtml = fs_1.readFileSync(path_1.join(__dirname, './test-files/header.html'), 'utf8');
const footerHtml = fs_1.readFileSync(path_1.join(__dirname, './test-files/footer.html'), 'utf8');
const firstPageHtml = fs_1.readFileSync(path_1.join(__dirname, './test-files/first-page.html'), 'utf8');
const resultsHtml = fs_1.readFileSync(path_1.join(__dirname, './test-files/results.html'), 'utf8');
const fontPath = path_1.join(__dirname, '../fonts/roboto-v18-latin_greek_cyrillic-regular.ttf');
const footerFontSize = index_1.Millimeters.of(4.2);
const margin = types_1.PrintMargin.ofMillimeters({
top: 7.5,
right: 15,
bottom: 5,
left: 17,
});
const TEST_TIMEOUT = 10000; // 10 seconds
const OUTPUT_NEW_EXPECTED_FILES = false;
let converter;
beforeAll(() => __awaiter(this, void 0, void 0, function* () {
// TODO: find out how to run without those options
// Travis-CI Docker image workaround
const puppeteerOptions = {
puppeteer: {
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-dev-shm-usage'],
},
};
converter = yield index_1.initialize(puppeteerOptions);
}));
afterAll(() => __awaiter(this, void 0, void 0, function* () {
yield converter.destroy();
}));
describe('mkCompoundPdf()', () => {
it('text footer', () => __awaiter(this, void 0, void 0, function* () {
const pdfBuffer = yield converter.mkCompoundPdf([
{
pdfContent: firstPageHtml,
footer: {
type: 'TextSlot',
left: {
fontPath: fontPath,
text: 'https://github.com/moshensky/html-to-pdf-converter',
size: footerFontSize,
underline: true,
color: 0x0060bf,
},
right: {
fontPath: fontPath,
text: 'Page {page} of {pages}',
size: footerFontSize,
color: 0x000000,
},
},
margin,
pageSize: types_1.pageSizeInMM.A4.portrait,
},
]);
yield test_helpers_1.compareToExpected('textFooter', pdfBuffer, OUTPUT_NEW_EXPECTED_FILES);
}), TEST_TIMEOUT);
// TODO: create different footer in order to see obvious differences
it('multiple pages with different html footers', () => __awaiter(this, void 0, void 0, function* () {
const pdfBuffer = yield converter.mkCompoundPdf([
{
pdfContent: firstPageHtml,
footer: {
type: 'HtmlSlot',
html: footerHtml,
},
margin,
pageSize: types_1.pageSizeInMM.A4.portrait,
},
{
pdfContent: resultsHtml,
footer: {
type: 'HtmlSlot',
html: footerHtml,
},
margin,
pageSize: types_1.pageSizeInMM.A4.landscape,
},
]);
yield test_helpers_1.compareToExpectedMultiple('htmlFooter', pdfBuffer, OUTPUT_NEW_EXPECTED_FILES);
}), TEST_TIMEOUT);
it('single page with html header', () => __awaiter(this, void 0, void 0, function* () {
const pdfBuffer = yield converter.mkCompoundPdf([
{
pdfContent: firstPageHtml,
header: {
type: 'HtmlSlot',
html: headerHtml,
},
margin,
pageSize: types_1.pageSizeInMM.A4.portrait,
},
]);
yield test_helpers_1.compareToExpected('singlePageWithHtmlHeader', pdfBuffer, OUTPUT_NEW_EXPECTED_FILES);
}), TEST_TIMEOUT);
});
//# sourceMappingURL=compound-pdf.spec.js.map