我正在尝试使用 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 来释放保存的文件?
提前致谢!