i'm trying to develop a (offline) packaged apps with chrome 23+ which allows the user to generate and print a pdf-file. I tried various ways to achieve this, but none really works.
Using something like this, the browser/app freezes:
window.html (includes pdf.js (http://code.google.com/p/jspdf/) and genpdf.js (see below)):
....
<browser src="about:blank" width="1024" height="768"></browser>
genpdf.js:
var doc = new jsPDF();
doc.text(20, 20, 'foo');
doc.text(20, 30, 'bar');
document.querySelector('browser').src = window.webkitURL.createObjectURL(new Blob([doc.output()], {type: 'application/pdf'}));
This would be my preferred way to display the generated PDF, but with the window freezing the user can't print it.
Another way would be to save the PDF to the desktop:
chrome.fileSystem.chooseFile({type: 'saveFile'}, function(writableFileEntry) {
writableFileEntry.createWriter(function(writer) {
writer.onerror = function(e) {
console.log('writeend');
};
writer.onwriteend = function(e) {
console.log('writeend');
};
var doc = new jsPDF();
doc.text(20, 20, 'foo');
doc.text(20, 30, 'bar');
writer.write(new Blob([doc.output()], {type: 'application/pdf'}));
}, errorHandler);
});
This works, but the file on the desktop is locked until the app is closed. Is there any api-call i'm missing to free the saved file?
Thanks in advance!
In the second solution I believe you should provide the buffer size, so the writer will be able to know when the write is ended.
var docBuffer = doc.output();
writer.write(new Blob([docBuffer], {type: 'application/pdf', 'size': docBuffer.length}));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With