Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome Packaged Apps / pdf.js

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!

like image 496
Marc Harding Avatar asked Sep 12 '12 06:09

Marc Harding


1 Answers

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}));
like image 95
kbtz Avatar answered Oct 11 '22 03:10

kbtz