我最近开始学习python,我想将现有的html文件转换为pdf文件。 这很奇怪,但pdfkit似乎是python的pdf文档的唯一库。
import pdfkit pdfkit.from_file("C:\\Users\\user\Desktop\\table.html", "out.pdf")
发生错误: OSError: No wkhtmltopdf executable found: "b''"
如何在Windows上正确configuration这个库使它工作? 我无法得到它:(
看起来你需要安装wkhtmltopdf。 对于Windows,安装程序可以在https://wkhtmltopdf.org/downloads.html找到
也检查出这个家伙,谁是有同样的问题的职位: 无法使用python PDFKIT创建PDF错误:“找不到wkhtmltopdf可执行文件:”
我发现工作解决方案。 如果你想将文件转换为pdf格式,就不要使用python来达到这个目的。 您需要在本地/删除服务器上将DOMPDF库包含到您的php脚本中。 像这样的东西:
<?php // include autoloader require_once 'vendor/autoload.php'; // reference the Dompdf namespace use Dompdf\Dompdf; if (isset($_POST['html']) && !empty($_POST['html'])) { // instantiate and use the dompdf class $dompdf = new Dompdf(); $dompdf->loadHtml($_POST['html']); // (Optional) Setup the paper size and orientation $dompdf->setPaper('A4', 'landscape'); // Render the HTML as PDF $dompdf->render(); // Output the generated PDF to Browser $dompdf->stream(); } else { exit(); }
然后在你的Python脚本,你可以发布你的HTML或任何内容到您的服务器,并获得生成的PDF文件作为回应。 像这样的东西:
import requests url = 'http://example.com/html2pdf.php' html = '<h1>hello</h1>' r = requests.post(url, data={'html': html}, stream=True) f = open('converted.pdf', 'wb') f.write(r.content) f.close()