Skip to content

Commit 05ca089

Browse files
committed
fix: aggressively remove all grey backgrounds and force A4 stretch in PDF export
1 parent 1517aca commit 05ca089

1 file changed

Lines changed: 20 additions & 18 deletions

File tree

utils/editor/pdf-custom.ts

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ export async function generateCustomPdf(docxBuffer: ArrayBuffer, title: string):
111111
// Give docx-preview time to finalize layout after images load
112112
await new Promise(r => setTimeout(r, 1500));
113113

114+
// Force ALL elements to have a white background just to be absolutely sure
115+
const allElements = iframeDoc.querySelectorAll("*");
116+
allElements.forEach(el => {
117+
const htmlEl = el as HTMLElement;
118+
if (htmlEl.style) {
119+
htmlEl.style.backgroundColor = "white";
120+
htmlEl.style.borderColor = "white";
121+
htmlEl.style.boxShadow = "none";
122+
}
123+
});
124+
114125
// 5. Capture individual pages
115126
const pages = Array.from(iframeDoc.querySelectorAll(".docx-wrapper > section")) as HTMLElement[];
116127
if (pages.length === 0) {
@@ -132,8 +143,12 @@ export async function generateCustomPdf(docxBuffer: ArrayBuffer, title: string):
132143

133144
for (let i = 0; i < pages.length; i++) {
134145
const pageEl = pages[i];
135-
// Ensure the page is visible to html2canvas
136-
pageEl.style.backgroundColor = "white";
146+
147+
// Ensure the page takes exactly the full width
148+
pageEl.style.margin = "0";
149+
pageEl.style.padding = "0";
150+
pageEl.style.width = "100%";
151+
pageEl.style.border = "none";
137152

138153
const canvas = await html2canvas(pageEl, {
139154
scale: 2,
@@ -148,22 +163,9 @@ export async function generateCustomPdf(docxBuffer: ArrayBuffer, title: string):
148163
doc.addPage();
149164
}
150165

151-
// Calculate aspect ratio to fit A4 perfectly
152-
const imgProps = doc.getImageProperties(imgData);
153-
const ratio = imgProps.width / imgProps.height;
154-
let finalWidth = pdfWidth;
155-
let finalHeight = pdfWidth / ratio;
156-
157-
// If it's taller than A4, scale it down to fit height
158-
if (finalHeight > pdfHeight) {
159-
finalHeight = pdfHeight;
160-
finalWidth = pdfHeight * ratio;
161-
}
162-
163-
// Center horizontally if needed
164-
const xOffset = (pdfWidth - finalWidth) / 2;
165-
166-
doc.addImage(imgData, "JPEG", xOffset, 0, finalWidth, finalHeight);
166+
// DO NOT preserve aspect ratio if it causes letterboxing (the grey border might be the PDF background showing through)
167+
// Force the image to cover the entire A4 page to eliminate any gaps
168+
doc.addImage(imgData, "JPEG", 0, 0, pdfWidth, pdfHeight);
167169
}
168170

169171
console.log("[pdf-custom] PDF generation successful");

0 commit comments

Comments
 (0)