简体   繁体   English

从JavaScript将HTML页面转换为PDF

[英]Convert an HTML page to PDF from JavaScript

I'm developing a Windows 8 note-taking app in HTML/JS. 我正在用HTML / JS开发Windows 8笔记应用程序。

I'm able to use a canvas overlay to get an image that I can export to, but there are alignment issues. 我可以使用画布覆盖获取可导出到的图像,但是存在对齐问题。 I'd prefer if there was a reliable way to export to PDF. 我希望有一种可靠的方法可以导出到PDF。 The stuff that needs exporting is some HTML embedded in a div. 需要导出的内容是在div中嵌入的一些HTML。

Note that I have the ability to save a byte stream, but I don't know how to convert this HTML to a PDF byte stream. 请注意,我可以保存字节流,但是我不知道如何将HTML转换为PDF字节流。 It's got to be done in JavaScript, but leveraging Windows 8 JS APIs is acceptable. 必须使用JavaScript来完成,但是可以使用Windows 8 JS API。

The document will contain MathJax and images, so there shouldn't be too many alignment issues. 该文档将包含MathJax和图像,因此应该不会有太多的对齐问题。

Ok for HTML -> PDF it's not easy, I mean you can manually save as a .pdf in a print dialog and thus supported by a @media print .css file. 对于HTML-> PDF,这并不容易,我的意思是,您可以在打印对话框中手动将其另存为.pdf,从而得到@media print .css文件的支持。

However I assume you want it to perform the change automatically, sifting through ridiculously priced commercial software I've managed to find the following... 但是我假设您希望它自动执行更改,筛选价格昂贵的商业软件,我设法找到了以下内容...

https://github.com/MrRio/jsPDF https://github.com/MrRio/jsPDF

By the looks of that, it should perform what you're looking for. 从外观上看,它应该可以满足您的需求。

Not sure about PDF but on on Windows 8 you can create an Image using the Webview.capturePreviewToBlobAsync() function. 不确定PDF,但在Windows 8上,您可以使用Webview.capturePreviewToBlobAsync()函数创建图像。

It captures everything that's displayed in the Webview and because it's a direct capture anything that can be displayed in IE11 can be captured including MathJax. 它捕获了Webview中显示的所有内容,并且由于它是直接捕获,因此可以捕获IE11中可以显示的任何内容,包括MathJax。

I wrote a simple sample app a moment ago and was able to capture the website: to an image. 我刚才写了一个简单的示例应用程序,能够捕获网站:图像。 Obviously you could use a URL or insert your own custom HTML. 显然,您可以使用URL或插入自己的自定义HTML。

function printeWebViewToDiskThenDisplay() {
        var wc = document.getElementById("webview");
        Windows.Storage.ApplicationData.current.localFolder.createFileAsync("webview.png", Windows.Storage.CreationCollisionOption.replaceExisting).then(function (file) {
            file.openAsync(Windows.Storage.FileAccessMode.readWrite).then(function (stream) {
                var capturePreview = wc.capturePreviewToBlobAsync();
                capturePreview.oncomplete = function (completeEvent) {
                    var inputStream = completeEvent.target.result.msDetachStream();
                    Windows.Storage.Streams.RandomAccessStream.copyAsync(inputStream, stream).then(function () {
                        stream.flushAsync().done(function () {
                            inputStream.close();
                            stream.close();
                            var image = new Image();
                            image.src = URL.createObjectURL(file);
                            result.innerHTML = "";
                            result.appendChild(image);
                        });
                    });
                };
                capturePreview.start();
            });
        });
    }

HTML looked something like this: HTML看起来像这样:

<button id="btnPrint">Print</button> 

<div style="transform:scale(1); transform-origin:0% 0%">
    <x-ms-webview id="webview" src="http://cdn.mathjax.org/mathjax/latest/test/sample.html"
                  style="width: 400px; height: 400px;">
    </x-ms-webview>
</div>
<div id="result"></div> 

The result was the following: 结果如下:

Windows 8应用

You can download the rough sample I built here: 您可以下载我在这里构建的粗略示例:

https://github.com/thebeebs/ASingleWebviewToImage https://github.com/thebeebs/ASingleWebviewToImage

我已经使用jsPDF JS库在JS中生成PDF文件,但是根据您的描述,我不确定这是否是您要查找的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM