print file with appendix
This commit is contained in:
parent
9ef82adef7
commit
98ce39efc5
7 changed files with 97 additions and 24 deletions
|
@ -16,6 +16,8 @@ import { InspectionPointEnum } from "../../../enums/inspectionEnum";
|
|||
import multer from "multer";
|
||||
import { FileSystemHelper } from "../../../helpers/fileSystemHelper";
|
||||
import { PdfExport } from "../../../helpers/pdfExport";
|
||||
import { PDFDocument } from "pdf-lib";
|
||||
import sharp from "sharp";
|
||||
|
||||
/**
|
||||
* @description get all inspections sorted by id not having newer inspection
|
||||
|
@ -240,13 +242,37 @@ export async function finishInspection(req: Request, res: Response): Promise<any
|
|||
formattedInspection.inspectionPlan.title
|
||||
}_${new Date(formattedInspection.finished ?? "").toLocaleDateString("de-de")}`;
|
||||
|
||||
await PdfExport.renderFile({
|
||||
let inspectionPoints = [];
|
||||
for (const ip of formattedInspection.inspectionVersionedPlan.inspectionPoints.sort(
|
||||
(a, b) => (a.sort ?? 0) - (b.sort ?? 0)
|
||||
)) {
|
||||
let value = formattedInspection.checks.find((c) => c.inspectionPointId == ip.id).value;
|
||||
let image = "";
|
||||
if (ip.type == InspectionPointEnum.file && ip.others == "img") {
|
||||
const imagePath = FileSystemHelper.formatPath("inspection", inspection.id, value);
|
||||
let pngImageBytes = await sharp(imagePath).png().toBuffer();
|
||||
image = `data:image/png;base64,${pngImageBytes.toString("base64")}`;
|
||||
} else if (ip.type == InspectionPointEnum.oknok) {
|
||||
value = value ? "OK" : "Nicht OK";
|
||||
}
|
||||
inspectionPoints.push({
|
||||
title: ip.title,
|
||||
description: ip.description,
|
||||
type: ip.type,
|
||||
min: ip.min,
|
||||
max: ip.max,
|
||||
others: ip.others,
|
||||
value: value,
|
||||
image: image,
|
||||
});
|
||||
}
|
||||
|
||||
let pdf = await PdfExport.renderFile({
|
||||
template: "inspection",
|
||||
title,
|
||||
filename: "printout",
|
||||
folder: `inspection/${inspection.id}`,
|
||||
saveToDisk: false,
|
||||
data: {
|
||||
inspector: `${req.userId}`,
|
||||
inspector: `${req.lastname}, ${req.firstname}`,
|
||||
context: formattedInspection.context || "---",
|
||||
createdAt: formattedInspection.created,
|
||||
finishedAt: formattedInspection.finished ?? new Date(),
|
||||
|
@ -255,23 +281,55 @@ export async function finishInspection(req: Request, res: Response): Promise<any
|
|||
plan: formattedInspection.inspectionPlan,
|
||||
planVersion: formattedInspection.inspectionVersionedPlan.version,
|
||||
planTitle: formattedInspection.inspectionPlan.title,
|
||||
checks: formattedInspection.inspectionVersionedPlan.inspectionPoints
|
||||
.sort((a, b) => (a.sort ?? 0) - (b.sort ?? 0))
|
||||
.map((ip) => ({
|
||||
title: ip.title,
|
||||
description: ip.description,
|
||||
type: ip.type,
|
||||
min: ip.min,
|
||||
max: ip.max,
|
||||
value:
|
||||
ip.type == InspectionPointEnum.file
|
||||
? "siehe Anhang"
|
||||
: formattedInspection.checks.find((c) => c.inspectionPointId == ip.id).value,
|
||||
})),
|
||||
checks: inspectionPoints,
|
||||
},
|
||||
});
|
||||
|
||||
// TODO: Anhang hinzufügen
|
||||
const finalDocument = await PDFDocument.create();
|
||||
const printout = await PDFDocument.load(pdf);
|
||||
const copiedPages = await finalDocument.copyPages(printout, printout.getPageIndices());
|
||||
copiedPages.forEach((page) => finalDocument.addPage(page));
|
||||
|
||||
let resultsForAppend = inspectionPoints.filter((ip) => ip.type == InspectionPointEnum.file && ip.others == "pdf");
|
||||
|
||||
if (resultsForAppend.length !== 0) {
|
||||
const appendixPage = finalDocument.addPage();
|
||||
const { width, height } = appendixPage.getSize();
|
||||
appendixPage.drawText("Anhang:", {
|
||||
x: 50,
|
||||
y: height - 50,
|
||||
size: 24,
|
||||
});
|
||||
}
|
||||
|
||||
for (const appendix of resultsForAppend) {
|
||||
const appendixPdfBytes = FileSystemHelper.readFileAsBase64("inspection", inspection.id, appendix.value);
|
||||
const appendixPdf = await PDFDocument.load(appendixPdfBytes);
|
||||
const appendixPages = await finalDocument.copyPages(appendixPdf, appendixPdf.getPageIndices());
|
||||
appendixPages.forEach((page) => finalDocument.addPage(page));
|
||||
|
||||
/** print image
|
||||
const imagePath = FileSystemHelper.formatPath("inspection", inspection.id, checkValue);
|
||||
let pngImageBytes = await sharp(imagePath).png().toBuffer();
|
||||
let image = await finalDocument.embedPng(pngImageBytes);
|
||||
let dims = image.scale(1);
|
||||
if (image) {
|
||||
const page = finalDocument.addPage();
|
||||
const { width, height } = page.getSize();
|
||||
const x = (width - dims.width) / 2;
|
||||
const y = (height - dims.height) / 2;
|
||||
page.drawImage(image, {
|
||||
x,
|
||||
y,
|
||||
width: dims.width,
|
||||
height: dims.height,
|
||||
});
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
const mergedPdfBytes = await finalDocument.save();
|
||||
FileSystemHelper.writeFile(`inspection/${inspection.id}`, `printout.pdf`, mergedPdfBytes);
|
||||
|
||||
let finish: FinishInspectionCommand = {
|
||||
id: inspectionId,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue