{"id":253042,"date":"2024-05-15T10:08:01","date_gmt":"2024-05-15T02:08:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/253042.html"},"modified":"2024-05-15T10:08:06","modified_gmt":"2024-05-15T02:08:06","slug":"%e5%a6%82%e4%bd%95%e7%94%a8%e4%bb%a3%e7%a0%81%e7%94%9f%e6%88%90pdf-3","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/253042.html","title":{"rendered":"\u5982\u4f55\u7528\u4ee3\u7801\u751f\u6210PDF"},"content":{"rendered":"<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/27091205\/5de270f4-1fa7-41db-8f24-48c7eec6d28d.webp\" alt=\"\u5982\u4f55\u7528\u4ee3\u7801\u751f\u6210PDF\" \/><\/p>\n<p><p>\u5728\u9700\u8981\u751f\u6210PDF\u6587\u4ef6\u7684\u573a\u666f\u4e2d\uff0c\u5229\u7528\u7f16\u7a0b\u6280\u672f\u751f\u6210PDF\u53ef\u4ee5\u4e3a\u7528\u6237\u5e26\u6765\u6781\u5927\u7684\u4fbf\u5229\u6027\u3002<strong>\u5e38\u7528\u7684\u4ee3\u7801\u751f\u6210PDF\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u6216API\u3001\u901a\u8fc7HTML\u8f6c\u6362\u548c\u5229\u7528\u7cfb\u7edf\u6253\u5370\u529f\u80fd<\/strong>\u3002\u5176\u4e2d\uff0c\u4f7f\u7528\u7279\u5b9a\u7684\u5e93\u6216API\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u5982Python\u7684ReportLab\u5e93\u3001Java\u7684iText\u5e93\u6216JavaScript\u7684jsPDF\u5e93\uff0c\u8fd9\u4e9b\u5e93\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u7f16\u7a0b\u63a5\u53e3\u6765\u521b\u5efaPDF\u6587\u6863\u3002\u4ee5Python\u7684ReportLab\u4e3a\u4f8b\uff0c\u8be5\u5e93\u5141\u8bb8\u521b\u5efa\u5305\u542b\u6587\u5b57\u3001\u56fe\u8868\u4ee5\u53ca\u56fe\u50cf\u7684\u590d\u6742PDF\u62a5\u544a\uff0c\u4f18\u70b9\u662f\u529f\u80fd\u5f3a\u5927\u4e14\u7075\u6d3b\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u9009\u62e9\u5408\u9002\u7684PDF\u751f\u6210\u5e93<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u751f\u6210PDF\u6587\u4ef6\uff0c\u9996\u5148\u9700\u8981\u9009\u62e9\u4e00\u4e2a\u5408\u9002\u7684\u7f16\u7a0b\u8bed\u8a00\u5e93\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u6d41\u884c\u7684\u5e93\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>Python\uff1aReportLab\u3001PyPDF2\u3001FPDF.<\/strong><\/li>\n<li>Java\uff1aiText\u3001Apache PDFBox.<\/li>\n<li>C#\uff1aiTextSharp\u3001PdfSharp.<\/li>\n<li>JavaScript\uff1ajsPDF\u3001PDFKit.<\/li>\n<\/ul>\n<p><p>\u4f7f\u7528ReportLab\u5728Python\u4e2d\u751f\u6210PDF\u662f\u4e00\u4e2a\u53d7\u6b22\u8fce\u7684\u9009\u62e9\u3002\u5b83\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u5e93\uff0c\u7528\u4e8e\u7f16\u7a0b\u521b\u5efa\u590d\u6742\u7684PDF\u6587\u6863\u548c\u81ea\u5b9a\u4e49PDF\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u5b89\u88c5\u548c\u8bbe\u7f6e\u6240\u9700\u7684\u5e93<\/h3>\n<\/p>\n<p><p>\u8981\u751f\u6210PDF\uff0c\u9996\u5148\u9700\u8981\u5728\u5f00\u53d1\u73af\u5883\u4e2d\u5b89\u88c5\u6240\u9009\u7684\u5e93\u3002\u4ee5ReportLab\u4e3a\u4f8b\uff0c\u53ef\u4ee5\u4f7f\u7528pip\u8fdb\u884c\u5b89\u88c5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pip install reportlab<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5b89\u88c5\u5b8c\u6210\u540e\uff0c\u53ef\u4ee5\u901a\u8fc7\u7b80\u5355\u7684\u811a\u672c\u6d4b\u8bd5\u5e93\u662f\u5426\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u4ee3\u7801\u751f\u6210PDF\u57fa\u672c\u6846\u67b6<\/h3>\n<\/p>\n<p><p>\u5728ReportLab\u4e2d\uff0c\u751f\u6210PDF\u7684\u57fa\u672c\u6846\u67b6\u6d89\u53ca\u521b\u5efa\u4e00\u4e2aPDF\u753b\u5e03\uff08Canvas\uff09\uff0c\u5728\u4e0a\u9762\u7ed8\u5236\u5185\u5bb9\uff0c\u7136\u540e\u4fdd\u5b58\u4e3a\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.pdfgen import canvas<\/p>\n<p>def create_pdf(path):<\/p>\n<p>    c = canvas.Canvas(path)<\/p>\n<p>    c.drawString(100, 750, &quot;Welcome to ReportLab!&quot;)  # \u5728\u5750\u6807(100, 750)\u5904\u6dfb\u52a0\u6587\u672c<\/p>\n<p>    c.save()<\/p>\n<p>create_pdf(&quot;hello.pdf&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u6dfb\u52a0\u9ad8\u7ea7\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u4e00\u65e6\u57fa\u672c\u6846\u67b6\u642d\u5efa\u5b8c\u6210\uff0c\u63a5\u4e0b\u6765\u53ef\u4ee5\u6dfb\u52a0\u9ad8\u7ea7\u5185\u5bb9\uff0c\u5982\u6dfb\u52a0\u56fe\u50cf\u3001\u8868\u683c\u3001\u56fe\u5f62\u7b49\u3002<\/p>\n<\/p>\n<p><h4>\u56fe\u7247\u548c\u56fe\u5f62<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.lib import colors<\/p>\n<p>from reportlab.graphics.shapes import Drawing, Line<\/p>\n<p>from reportlab.lib.units import inch<\/p>\n<h2><strong>\u6dfb\u52a0\u56fe\u5f62\u548c\u56fe\u50cf<\/strong><\/h2>\n<p>def draw_shapes():<\/p>\n<p>    d = Drawing(0, 0)<\/p>\n<p>    d.add(Line(50, 50, 300, 50, strokeColor=colors.green))<\/p>\n<p>    c = canvas.Canvas(&quot;shapes.pdf&quot;)<\/p>\n<p>    c.drawInlineImage(&quot;path\/to\/image.jpg&quot;, inch, inch, width=400, height=300)<\/p>\n<p>    d.drawOn(c, 0, 600)<\/p>\n<p>    c.save()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>\u8868\u683c<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from reportlab.platypus import SimpleDocTemplate, Table, TableStyle<\/p>\n<h2><strong>\u521b\u5efa\u8868\u683c<\/strong><\/h2>\n<p>def build_pdf(output_filename):<\/p>\n<p>    doc = SimpleDocTemplate(output_filename)<\/p>\n<p>    data = [[&#039;Item&#039;, &#039;Cost&#039;, &#039;Quantity&#039;],<\/p>\n<p>            [&#039;Widgets&#039;, &#039;$0.50&#039;, 1000],<\/p>\n<p>            [&#039;Gadgets&#039;, &#039;$1.00&#039;, 500]]<\/p>\n<p>    table = Table(data)<\/p>\n<p>    table.setStyle(TableStyle([(&#039;BACKGROUND&#039;, (0, 0), (2, 0), colors.green),<\/p>\n<p>                               (&#039;TEXTCOLOR&#039;, (0, 0), (-1, 0), colors.whitesmoke)]))<\/p>\n<p>    elements = [table]<\/p>\n<p>    doc.build(elements)<\/p>\n<p>build_pdf(&quot;table.pdf&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001HTML\u8f6cPDF<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u5c24\u5176\u662f\u7f51\u7edc\u5e94\u7528\u4e2d\uff0c\u6709\u9700\u8981\u5c06HTML\u76f4\u63a5\u8f6c\u6362\u6210PDF\u3002\u53ef\u4ee5\u4f7f\u7528\u5982wkhtmltopdf\u8fd9\u6837\u7684\u547d\u4ee4\u884c\u5de5\u5177\u6216\u5e93\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528wkhtmltopdf<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pdfkit<\/p>\n<p>pdfkit.from_url(&#039;http:\/\/google.com&#039;, &#039;out.pdf&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6216\u8005\u5c06HTML\u6587\u4ef6\u8f6c\u6362\u4e3aPDF\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">pdfkit.from_file(&#039;test.html&#039;, &#039;out.pdf&#039;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728JavaScript\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528PhantomJS\u6216\u8005Puppeteer\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u7ec6\u8282\u4f18\u5316\u548c\u6d4b\u8bd5<\/h3>\n<\/p>\n<p><p>\u5728\u5b8c\u6210PDF\u7684\u57fa\u672c\u6784\u5efa\u540e\uff0c\u5e94\u5f53\u8fdb\u884c\u7ec6\u8282\u4f18\u5316\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u786e\u4fdd\u5185\u5bb9\u9002\u5f53\u5206\u9875\u3002<\/li>\n<li>\u8bbe\u7f6e\u5408\u9002\u7684\u9875\u8fb9\u8ddd\u3002<\/li>\n<li>\u6267\u884c\u5b57\u4f53\u548c\u989c\u8272\u7684\u4e00\u81f4\u6027\u6d4b\u8bd5\u3002<\/li>\n<li>\u68c0\u67e5\u56fe\u50cf\u8d28\u91cf\u662f\u5426\u6ee1\u8db3\u8981\u6c42\u3002<\/li>\n<\/ol>\n<p><p>\u8fd9\u4e9b\u90fd\u53ef\u4ee5\u901a\u8fc7\u4e0d\u540c\u5e93\u63d0\u4f9b\u7684API\u5b8c\u6210\u3002\u6700\u7ec8\uff0c\u901a\u8fc7\u591a\u8f6e\u6d4b\u8bd5\u786e\u4fddPDF\u6587\u4ef6\u7684\u51c6\u786e\u6027\u548c\u4e13\u4e1a\u6027\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u96c6\u6210\u4e0e\u81ea\u52a8\u5316<\/h3>\n<\/p>\n<p><p>\u5c06PDF\u751f\u6210\u7684\u4ee3\u7801\u96c6\u6210\u5230\u5e94\u7528\u7a0b\u5e8f\u4e2d\u53ef\u4ee5\u901a\u8fc7\u6dfb\u52a0API\u63a5\u53e3\u6216\u8005\u4f5c\u4e3a\u4e00\u4e2a\u72ec\u7acb\u670d\u52a1\u8fd0\u884c\u3002\u5728\u62a5\u8868\u751f\u6210\u3001\u53d1\u7968\u6253\u5370\u3001\u7528\u6237\u5185\u5bb9\u5bfc\u51fa\u7b49\u529f\u80fd\u4e2d\uff0c\u81ea\u52a8\u751f\u6210PDF\u663e\u5f97\u5c24\u4e3a\u91cd\u8981\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u5b9a\u65f6\u4efb\u52a1\u6216\u4e8b\u4ef6\u89e6\u53d1\u8fdb\u884cPDF\u751f\u6210\u7684\u81ea\u52a8\u5316\uff0c\u53ef\u8fdb\u4e00\u6b65\u63d0\u5347\u6548\u7387\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u8bbe\u7f6e\u5b9a\u65f6\u751f\u6210\u62a5\u8868\u7684Cron\u4efb\u52a1\u3002<\/li>\n<li>\u901a\u8fc7Webhooks\u5728\u6570\u636e\u66f4\u65b0\u65f6\u751f\u6210PDF\u3002<\/li>\n<\/ol>\n<p><p>\u5b8c\u6210\u4ee5\u4e0a\u6b65\u9aa4\u540e\uff0c\u5c31\u53ef\u4ee5\u5728\u5404\u79cd\u573a\u666f\u4e0b\u6839\u636e\u9700\u6c42\u751f\u6210\u7ed3\u6784\u5316\u548c\u4e2a\u6027\u5316\u7684PDF\u6587\u6863\u4e86\u3002\u6700\u91cd\u8981\u7684\u662f\u4e0d\u65ad\u8fed\u4ee3\u4e0e\u4f18\u5316\u4ee5\u6ee1\u8db3\u7528\u6237\u7684\u9700\u6c42\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p><strong>1. \u6211\u8be5\u4f7f\u7528\u54ea\u79cd\u7f16\u7a0b\u8bed\u8a00\u6765\u751f\u6210PDF\u6587\u4ef6\uff1f<\/strong><br \/>\n\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u7f16\u7a0b\u8bed\u8a00\u6765\u751f\u6210PDF\u6587\u4ef6\uff0c\u6bd4\u8f83\u5e38\u89c1\u7684\u6709Java\u3001Python\u3001C# \u548cRuby\u7b49\u3002\u9009\u62e9\u54ea\u79cd\u8bed\u8a00\u4e3b\u8981\u53d6\u51b3\u4e8e\u60a8\u719f\u6089\u7684\u7f16\u7a0b\u8bed\u8a00\u4ee5\u53ca\u9879\u76ee\u7684\u9700\u6c42\u3002<\/p>\n<p><strong>2. \u6211\u9700\u8981\u4f7f\u7528\u54ea\u4e2a\u5e93\u6216\u6846\u67b6\u6765\u751f\u6210PDF\u6587\u4ef6\uff1f<\/strong><br \/>\n\u6709\u8bb8\u591a\u6d41\u884c\u7684\u5e93\u548c\u6846\u67b6\u53ef\u4f9b\u9009\u62e9\uff0c\u53ef\u4ee5\u5e2e\u52a9\u60a8\u751f\u6210PDF\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u5728Java\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528iText\u6216Apache PDFBox\uff1b\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528ReportLab\u6216PyPDF2\uff1b\u5728C#\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528iTextSharp\u6216PdfSharp\u7b49\u3002<\/p>\n<p><strong>3. \u6211\u5e94\u8be5\u91c7\u7528\u4ec0\u4e48\u65b9\u6cd5\u6765\u751f\u6210PDF\uff1f<\/strong><br \/>\n\u751f\u6210PDF\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u60a8\u53ef\u4ee5\u9009\u62e9\u9002\u5408\u60a8\u9879\u76ee\u9700\u6c42\u7684\u65b9\u6cd5\u3002\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u73b0\u6709\u7684PDF\u6a21\u677f\uff0c\u7136\u540e\u5728\u6a21\u677f\u4e0a\u586b\u5145\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u6cd5\u5728\u751f\u6210\u91cd\u590d\u683c\u5f0f\u7684\u6587\u6863\u65f6\u975e\u5e38\u6709\u7528\u3002\u53e6\u4e00\u79cd\u65b9\u6cd5\u662f\u901a\u8fc7\u4ee3\u7801\u9010\u6b65\u521b\u5efaPDF\u6587\u4ef6\uff0c\u4f8b\u5982\u6dfb\u52a0\u6587\u672c\u3001\u56fe\u7247\u3001\u8868\u683c\u7b49\u3002\u8fd9\u79cd\u65b9\u6cd5\u66f4\u52a0\u7075\u6d3b\uff0c\u4f46\u53ef\u80fd\u9700\u8981\u66f4\u591a\u7684\u5f00\u53d1\u5de5\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728\u9700\u8981\u751f\u6210PDF\u6587\u4ef6\u7684\u573a\u666f\u4e2d\uff0c\u5229\u7528\u7f16\u7a0b\u6280\u672f\u751f\u6210PDF\u53ef\u4ee5\u4e3a\u7528\u6237\u5e26\u6765\u6781\u5927\u7684\u4fbf\u5229\u6027\u3002\u5e38\u7528\u7684\u4ee3\u7801\u751f\u6210PDF\u7684\u65b9\u6cd5\u5305\u62ec [&hellip;]","protected":false},"author":3,"featured_media":253045,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/253042"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=253042"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/253042\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/253045"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=253042"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=253042"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=253042"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}