如果您可以从 URL 检索 HTML,您可能需要检查这个答案 https://stackoverflow.com/questions/25046187/how-to-generate-a-pdf-from-html-web-page/48608455#48608455我写的解释了如何做。
Example:
https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=show
在 HTML 中放置一个锚点来显示 PDF 或在 HTML 中下载它非常容易。
该锚点将显示从https://www.github.com
page:
<a href="https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=show" target="_blank">Show PDF</a>
这个将下载它:
<a href="https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=download&file_name=my_pdf">Download PDF</a>
如果您想使用 JavaScript 进行尝试,您可以创建一个 HTML 按钮并在单击事件中下载 PDF。例子:
HTML:
<button type="button" id="download-pdf-button">Download the PDF</button>
JavaScript:
document.getElementById("download-pdf-button").addEventListener("click", function() {
var link = document.createElement('a');
link.href = 'https://dhtml2pdf.herokuapp.com/api.php?url=https://www.github.com&result_type=download';
link.download = 'file.pdf';
link.dispatchEvent(new MouseEvent('click'));
});
查看Github中的项目 https://github.com/Dellos7/dhtml2pdf.
希望能帮助到你。