使用JS将网页直接保存为PDF

Abr*_*hin 20 html javascript pdf html5

我想将网页直接保存为PDF.

我所做的是 -

<form>
    <input type=button name=print value="Print" onClick="window.print()">
</form>
Run Code Online (Sandbox Code Playgroud)

但它为我提供了打印选项或将页面保存为PDF.

但我想要的是当我点击按钮时,它直接将页面保存为PDF而不显示任何选项.

JavaScript中有任何解决方案吗?

在此先感谢您的帮助.

jpe*_*zov 5

简短的回答是否定的,你不能阻止用户只使用javascript在他们的浏览器中看到该选项.

稍微长一点的答案是,你可以用比javascript更多的东西做到这一点.

使用html2canvas等服务,您可以向服务器上的页面发送POST请求.使用该页面将图像转换为PDF,并将其作为下载输出.

假设你正在使用PHP:

<?php
header("Content-type:application/pdf");
header("Content-Disposition:attachment;filename='screen-shot.pdf'");
// The above headers will cause the file to automatically be downloaded.
// Use a library to convert the image to a PDF here.
Run Code Online (Sandbox Code Playgroud)

将图像转换为PDF的示例库是mPDFTCPDF.随意谷歌其他人,特别是如果你不使用PHP.

请注意,这个解决方案不如他们自己做出选择,因为质量肯定不会那么好.