6

我正在尝试使用 chrome 23+ 开发(离线)打包应用程序,它允许用户生成和打印 pdf 文件。我尝试了各种方法来实现这一点,但没有一个真正有效。

使用这样的东西,浏览器/应用程序冻结:

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'}));

这将是我显示生成的 PDF 的首选方式,但由于窗口冻结,用户无法打印它。

另一种方法是将 PDF 保存到桌面:

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);
});

这有效,但桌面上的文件被锁定,直到应用程序关闭。我是否缺少任何 api-call 来释放保存的文件?

提前致谢!

4

2 回答 2

3

在第二种解决方案中,我相信您应该提供缓冲区大小,以便编写者能够知道写入何时结束。

var docBuffer = doc.output();
writer.write(new Blob([docBuffer], {type: 'application/pdf', 'size': docBuffer.length}));
于 2012-12-11T18:33:41.813 回答
-1

抢先看一下 wkhtmltopdf,它使用 WebKit 引擎从 HTML+CSS 页面生成 PDF。我喜欢它,它产生与 Chromium 浏览器中显示的页面相同的像素结果。有 MS-Windows 和 Linux 的版本,我都试过了。

于 2012-12-13T22:20:31.723 回答