使用window.print内容以pdf格式下载网页
I would like to have a link that when is clicked, automatically starts the download of the printable version of the web page.
I'm using Moodle. The content I want it's exactly the same If I download the page using ctrl + p and saving as pdf or using
<a href=\"whatever.htm\" onClick=\"window.print();return false\">Download web page</a>
I want exactly that content because using this way, the header, sidebar and footer is removed. I don't want css.
I'm not using this because this doesn't work in some browsers.
I'd rather not using pdf libraries like tcpdf because Moodle loads the content in a dynamic way and send this to a libraries like that is a mess.
I tried using sites like pdfcrowd.com but this kind of sites don't work when the site you want to convert to pdf uses a server-side session to identify the user.
我想有一个链接点击时,会自动开始下载网页的可打印版本 。 p>
我正在使用Moodle。 我希望它的内容完全相同如果我使用ctrl + p下载页面并保存为pdf或使用 p>
&lt; a href = \“whatever.htm \”onClick = \“window.print();返回false \”&gt;下载网页&lt; / a&gt;
code> pre>
我想要那个内容,因为使用这种方式, 标题,侧边栏和页脚已删除。 我不想要CSS。 p>
我没有使用它,因为这在某些浏览器中不起作用。 p>
我不想 使用像tcpdf这样的pdf库,因为Moodle以动态的方式加载内容并将其发送到类似的库中。 p>
我尝试使用像pdfcrowd.com这样的网站但这种网站 当您要转换为pdf的站点使用服务器端会话来识别用户时,不起作用。 p>
div>
I don't believe that there is a way to do this with window.print()
. However, there are HTML to PDF converters available for free, and you could automatically start a download with that. An example of this would be jsPDF, a free library for converting HTML to a PDF with Javascript
**There is easy ways to use jsPDF its very simple just you will use JSpdf Library **
<script src="https://cdnjs.cloudflare.com/ajax/libs/jspdf/1.3.2/jspdf.min.js"></script>
function onClick() {
var pdf = new jsPDF('p', 'pt', 'letter');
pdf.canvas.height = 72 * 11;
pdf.canvas.width = 72 * 8.5;
pdf.fromHTML(document.body);
pdf.save('test.pdf');
};
var element = document.getElementById("clickbind");
element.addEventListener("click", onClick);