Skip to content

Commit 469d1ec

Browse files
authored
Merge pull request #3 from H1D/fix/firefox-pdf-preview
Fix PDF preview in Firefox/Waterfox
2 parents 8b1ba5e + 1ac2a55 commit 469d1ec

File tree

2 files changed

+9
-29
lines changed

2 files changed

+9
-29
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ screenshots/
99
explore-site.mjs
1010
public/js/app.js
1111
public/js/app.js.map
12+
13+
# Local Netlify folder
14+
.netlify

public/js/pdf-generator.ts

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ declare const jspdf: { jsPDF: any };
99
declare const openSansRegularFont: string;
1010
declare const openSansBoldFont: string;
1111

12-
// Module-level variable to track the previous preview URL for cleanup
13-
let previousPreviewUrl: string | null = null;
1412

1513
/** Register Open Sans (Regular + Bold) with a jsPDF instance. */
1614
function registerFonts(doc: any): void {
@@ -855,37 +853,16 @@ export function downloadPdf(data: InvoiceData): void {
855853
}
856854

857855
/**
858-
* Generate the PDF blob, create an object URL, and set it as the src
859-
* on the #pdf-preview iframe. Revokes the previous URL to prevent
860-
* memory leaks.
856+
* Generate the PDF and set it as the src on the #pdf-preview iframe
857+
* using a data URL. This avoids the blob URL approach which causes
858+
* Firefox/Waterfox to download the PDF instead of displaying it inline.
861859
*/
862860
export function updatePreview(data: InvoiceData): void {
863-
const blob = generatePdfBlob(data);
864-
const url = URL.createObjectURL(blob);
861+
const doc = generatePdf(data);
862+
const dataUrl = doc.output('dataurlstring') as string;
865863

866864
const iframe = document.getElementById('pdf-preview') as HTMLIFrameElement | null;
867865
if (iframe) {
868-
iframe.src = url;
869-
// Once loaded, force the iframe's internal document to fill available space
870-
iframe.onload = () => {
871-
try {
872-
const doc = iframe.contentDocument;
873-
if (doc?.documentElement) {
874-
doc.documentElement.style.height = '100%';
875-
doc.documentElement.style.overflow = 'hidden';
876-
}
877-
if (doc?.body) {
878-
doc.body.style.height = '100%';
879-
doc.body.style.overflow = 'hidden';
880-
doc.body.style.margin = '0';
881-
}
882-
} catch (_) { /* cross-origin — ignore */ }
883-
};
884-
}
885-
886-
// Revoke the previous URL to prevent memory leaks
887-
if (previousPreviewUrl) {
888-
URL.revokeObjectURL(previousPreviewUrl);
866+
iframe.src = dataUrl;
889867
}
890-
previousPreviewUrl = url;
891868
}

0 commit comments

Comments
 (0)