我找到工作的解决方案 如果你想将文件转换为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()
pdfkit不是从Python生成PDF的唯一选项。 [Sphinx](http://www.sphinx-doc.org)是一个非常强大的文档生成器,可以使用LaTeX,[rinohtype](http://www.mos6581.org/rinohtype/)或[rst2pdf]输出PDF (http://rst2pdf.ralsina.me)(还有许多其他格式,还有一些可以从HTML生成PDF的其他Python软件包,例如[Weasyprint](http://weasyprint.org)) –