使用JS将网页直接保存为PDF [英] Save Web page directly to PDF using JS

查看:855
本文介绍了使用JS将网页直接保存为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to save web page directly to PDF.

我所做的是 -

<form>
    <input type=button name=print value="Print" onClick="window.print()">
</form>

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

But it gives me option for print or save the page as PDF.

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

But what I want is when I click on the button, it directly save the page as PDF not showing any option.

JavaScript中是否有任何解决方案?

Is there any solution in JavaScript?

提前感谢您的帮助。

推荐答案

简短的回答是否定的,您不能阻止用户看到浏览器中只使用javascript的选项。

The short answer is no, you cannot prevent users from seeing the option in their browser using just javascript.

稍微长一点的答案是,可以比javascript更多地执行此操作。

The slightly-longer answer, is that you can do this with a bit more than javascript.

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

Using a service such as html2canvas, you can send a POST request to a page on your server. Use that page to convert the image to a PDF, and have it output the file as a download.

假设您正在使用PHP:

Assuming you're using 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.

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

An example library to convert an image to a PDF is mPDF, or TCPDF. Feel free to Google others, especially if you're not using PHP.

请注意,这个解决方案不如他们自己做出选择,因为质量绝对赢了不太好。

Do note that this solution is inferior to them just making the choice themselves, as the quality definitely won't be as nice.

这篇关于使用JS将网页直接保存为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆