fix: pdf printing with puppeteer
This commit is contained in:
parent
4385b6dc34
commit
98477eafde
6 changed files with 1023 additions and 733 deletions
|
@ -1,21 +1,6 @@
|
|||
import { readFileSync } from "fs";
|
||||
import pdf, { Options } from "pdf-creator-node";
|
||||
|
||||
var options = (title: string = "pdf-export Mitgliederverwaltung"): Options => ({
|
||||
format: "A4",
|
||||
orientation: "portrait",
|
||||
border: "10mm",
|
||||
header: {
|
||||
height: "10mm",
|
||||
contents: `<h1 style="text-align: center;">${title}</h1>`,
|
||||
},
|
||||
footer: {
|
||||
height: "5mm",
|
||||
contents: {
|
||||
default: '<span style="color: #444;">{{page}}</span>/<span>{{pages}}</span>',
|
||||
},
|
||||
},
|
||||
});
|
||||
import Handlebars from "handlebars";
|
||||
import puppeteer from "puppeteer";
|
||||
|
||||
export abstract class PdfExport {
|
||||
static getTemplate(template: string) {
|
||||
|
@ -24,7 +9,7 @@ export abstract class PdfExport {
|
|||
|
||||
static async renderFile({
|
||||
template,
|
||||
title,
|
||||
title = "pdf-export Mitgliederverwaltung",
|
||||
filename,
|
||||
data,
|
||||
}: {
|
||||
|
@ -33,12 +18,36 @@ export abstract class PdfExport {
|
|||
filename: string;
|
||||
data: any;
|
||||
}) {
|
||||
let document = {
|
||||
html: this.getTemplate(template),
|
||||
data,
|
||||
path: process.cwd() + `/export/${filename}.pdf`,
|
||||
};
|
||||
const templateHtml = this.getTemplate(template);
|
||||
const templateCompiled = Handlebars.compile(templateHtml);
|
||||
const html = templateCompiled(data);
|
||||
|
||||
await pdf.create(document, options(title));
|
||||
const browser = await puppeteer.launch({
|
||||
headless: true,
|
||||
args: ["--no-sandbox", "--disable-gpu", "--disable-setuid-sandbox"],
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
await page.setContent(html, { waitUntil: "domcontentloaded" });
|
||||
|
||||
await page.pdf({
|
||||
path: process.cwd() + `/export/${filename}.pdf`, // Name der PDF-Datei
|
||||
format: "A4",
|
||||
printBackground: false,
|
||||
margin: {
|
||||
top: "15mm",
|
||||
bottom: "15mm",
|
||||
left: "10mm",
|
||||
right: "10mm",
|
||||
},
|
||||
displayHeaderFooter: true,
|
||||
headerTemplate: `<h1 style="font-size:10px; text-align:center; width:100%;">${title}</h1>`,
|
||||
footerTemplate: `
|
||||
<div style="font-size:10px; text-align:center; width:100%; color:#888;">
|
||||
Seite <span class="pageNumber"></span> von <span class="totalPages"></span>
|
||||
</div>
|
||||
`,
|
||||
});
|
||||
|
||||
await browser.close();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue