Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty PDF issue #2833

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions examples/basics.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ var now = new Date();
var pdf = pdfmake.createPdf(docDefinition);
pdf.write('pdfs/basics.pdf').then(() => {
console.log(new Date() - now);
// Add a verification step to check the content of the generated PDF
pdf.getBuffer().then(buffer => {
if (buffer.byteLength === 0) {
console.error('Empty PDF content');
} else {
console.log('PDF content is correctly embedded');
}
}).catch(err => {
console.error(err);
});
}, err => {
console.error(err);
});
10 changes: 10 additions & 0 deletions examples/columns_simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ var now = new Date();
var pdf = pdfmake.createPdf(docDefinition);
pdf.write('pdfs/columns_simple.pdf').then(() => {
console.log(new Date() - now);
// Add a verification step to check the content of the generated PDF
pdf.getBuffer().then(buffer => {
if (buffer.byteLength === 0) {
console.error('Empty PDF content');
} else {
console.log('PDF content is correctly embedded');
}
}).catch(err => {
console.error(err);
});
}, err => {
console.error(err);
});
13 changes: 12 additions & 1 deletion src/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,18 @@ class pdfmake {
let printer = new Printer(this.fonts, this.virtualfs, this.urlResolver());
const pdfDocumentPromise = printer.createPdfKitDocument(docDefinition, options);

return this._transformToDocument(pdfDocumentPromise);
// Add a verification step to ensure the content is correctly embedded
return pdfDocumentPromise.then(pdfDocument => {
return new Promise((resolve, reject) => {
pdfDocument.getBuffer().then(buffer => {
if (buffer.byteLength === 0) {
reject(new Error('Empty PDF content'));
} else {
resolve(this._transformToDocument(pdfDocument));
}
}).catch(reject);
});
});
}

setProgressCallback(callback) {
Expand Down
10 changes: 8 additions & 2 deletions src/browser-extensions/OutputDocumentBrowser.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,14 @@ class OutputDocumentBrowser extends OutputDocument {
return new Promise((resolve, reject) => {
this.getBlob().then(blob => {
try {
saveAs(blob, filename);
resolve();
// Ensure the content is correctly embedded before saving the file
this.getBuffer().then(buffer => {
if (buffer.byteLength === 0) {
throw new Error('Empty PDF content');
}
saveAs(blob, filename);
resolve();
});
} catch (e) {
reject(e);
}
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/Node-interface.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,30 @@ describe('Node interface', function () {
});

});

describe('download', function () {
it('should download PDF with content', function (done) {
var docDefinition = {
content: [
'First paragraph',
'Another paragraph, this time a little bit longer to make sure, this line will be divided into at least two lines'
]
};

var pdf = pdfmake.createPdf(docDefinition);
pdf.download('test.pdf').then(() => {
pdf.getBuffer().then(buffer => {
if (buffer.byteLength === 0) {
throw new Error('Empty PDF content');
} else {
done();
}
}).catch(err => {
throw err;
});
}).catch(err => {
throw err;
});
});
});
});
Loading