Added infinite height support.

This commit is contained in:
Tom Butcher 2025-12-13 21:02:14 +00:00
parent aa4d2c6873
commit 4653430c0f
4 changed files with 38 additions and 15 deletions

View File

@ -18,18 +18,18 @@
} }
.previewWrapper { .previewWrapper {
width: <%= (width * scale) + 'mm' || '50mm' %>; width: <%= (scaledWidth) || '50mm' %>;
height: <%= (height * scale) + 'mm' || '50mm' %>; height: <%= (scaledHeight) || '50mm' %>;
} }
.previewDocument { .previewDocument {
width: <%= (width) + 'mm' || '50mm' %>; width: <%= (width) || '50mm' %>;
height: <%= (height) + 'mm' || '50mm' %>; height: <%= (height) || '50mm' %>;
transform: scale(<%= scale || '1' %>); transform: scale(<%= scale || '1' %>);
transform-origin: top left; transform-origin: top left;
} }
.renderDocument { .renderDocument {
width: <%= (width * scale) + 'mm' || '50mm' %>; width: <%= (scaledWidth) || '50mm' %>;
height: <%= (height * scale) + 'mm' || '50mm' %>; height: <%= (scaledHeight) || '50mm' %>;
transform: scale(<%= scale || '1' %>); transform: scale(<%= scale || '1' %>);
} }
</style> </style>

View File

@ -1 +1 @@
<div class="renderDocument"><%- content %></div> <div class="renderDocument" id="content"><%- content %></div>

View File

@ -30,13 +30,22 @@ export async function generatePDF(html, options = {}) {
waitUntil: 'networkidle0' waitUntil: 'networkidle0'
}); });
var height = `${options?.height || '50'}mm`;
if (options.height == 'auto') {
console.log('Calculating height');
const calculatedHeight = await page.evaluate(() => {
return document.getElementById('content').scrollHeight;
});
height = `${calculatedHeight}px`;
}
// Generate PDF with specified dimensions // Generate PDF with specified dimensions
const pdfBuffer = await page.pdf({ const pdfBuffer = await page.pdf({
format: options.format || undefined,
width: options.width ? `${options.width}mm` : undefined,
height: options.height ? `${options.height}mm` : undefined,
printBackground: true, printBackground: true,
preferCSSPageSize: true, preferCSSPageSize: true,
width: options.width ? `${options.width}mm` : undefined,
height: height ? `${height}` : undefined,
margin: { margin: {
top: '0mm', top: '0mm',
right: '0mm', right: '0mm',

View File

@ -430,8 +430,14 @@ export class TemplateManager {
documentTemplate.parent.content == null || documentTemplate.parent.content == null ||
typeof documentTemplate.parent.content !== 'string' typeof documentTemplate.parent.content !== 'string'
) { ) {
console.log(
'Parent template content is required and must be a string.',
documentTemplate.parent.content
);
return { return {
error: 'Parent template content is required and must be a string.' error:
'Parent template content is required and must be a string.' +
documentTemplate.parent.content
}; };
} }
templateWithParentContent = await ejs.render( templateWithParentContent = await ejs.render(
@ -481,12 +487,18 @@ export class TemplateManager {
return { error: 'Failed to render inner template content.' }; return { error: 'Failed to render inner template content.' };
} }
const infiniteHeight = documentSize.infiniteHeight == true;
const baseHtml = await ejs.render( const baseHtml = await ejs.render(
baseTemplate, baseTemplate,
{ {
content: innerHtml, content: innerHtml,
width: documentSize.width, width: `${documentSize.width}mm`,
height: documentSize.height, height: infiniteHeight ? 'fit-content' : `${documentSize.height}mm`,
scaledWidth: `${documentSize.width * scale}mm`,
scaledHeight: infiniteHeight
? 'auto'
: `${documentSize.height * scale}mm`,
scale: `${scale}`, scale: `${scale}`,
baseCSS: baseCSS, baseCSS: baseCSS,
previewPaginationScript: preview ? previewPaginationScript : '' previewPaginationScript: preview ? previewPaginationScript : ''
@ -497,7 +509,8 @@ export class TemplateManager {
const previewObject = { const previewObject = {
html: baseHtml, html: baseHtml,
width: documentSize.width, width: documentSize.width,
height: documentSize.height height: infiniteHeight ? 'auto' : documentSize.height,
infiniteHeight: infiniteHeight
}; };
return previewObject; return previewObject;
@ -552,7 +565,8 @@ export class TemplateManager {
// Generate PDF using PDF factory // Generate PDF using PDF factory
const pdfBuffer = await generatePDF(baseHtml, { const pdfBuffer = await generatePDF(baseHtml, {
width: renderedTemplate.width, width: renderedTemplate.width,
height: renderedTemplate.height height: renderedTemplate.height,
infiniteHeight: renderedTemplate.infiniteHeight
}); });
const pdfObject = { const pdfObject = {