【问题标题】:convert html div or page url to pdf using php使用 php 将 html div 或页面 url 转换为 pdf
【发布时间】:2012-12-11 08:08:58
【问题描述】:

我有一个这样的 HTML 代码:

<div id="invoice"> 
  <div id="invoice-header"> <img alt="Mainlogo_large" class="logo screen" src="http://colosus.com/invoice/images/freeagent_logo.png"> 
    <!-- hCard microformat --> 
    <div class="vcard" id="company-address"> 
      <div class="fn org"><strong>KIOSBAN.COM</strong></div> 
      <div class="adr"> 
        <div class="street-address">Gedung Atlantica Lt.4<br>
          Jl. Kuningan Barat No. 7, Mampang<br> 
        </div> 
        <!-- street-address --> 
        <div class="locality">Indonesia</div> 
        <div id="company-postcode"><span class="region">Jakarta Selatan</span> <span class="postal-code">12710</span></div> 
      </div> 
      <!-- adr --> 
      <div class="email">fajar@kiosban.com</div> 
    <div id="sales-tax-reg-number">Fajar tampan</div> 
    </div> 
    <!-- company-address vcard --> 
  </div> 
  <!-- #invoice-header --> 
  <div id="invoice-info"> 
    <h2>Invoice <strong>INV001</strong></h2> 
    <h3>21 February 2008</h3> 
    <p id="payment-terms">Payment Terms: 30 days</p> 
    <p id="payment-due">Payment due by 21 March 2008</p> 
    <p id="payment-total">£2643.75</p>
  </div> 
  <!-- #invoice-info --> 
  <div class="vcard" id="client-details"> 
    <div class="fn">John Doe</div> 
    <div class="org">Client Company</div> 
    <div class="adr"> 
      <div class="street-address"> Client Street Address<br> 
        Street Address 2<br> 
        Street Address 3<br> 
      </div> 
      <!-- street-address --> 
      <div class="locality">LOCALITY</div> 
      <div id="client-postcode"><span class="region">Region</span> <span class="postal-code">MV2 8SX</span></div> 
      <div id="your-tax-number">SALES TAX: 193528491</div>
    </div> 
    <!-- adr --> 
  </div> 
  <!-- #client-details vcard --> 
  <table id="invoice-amount"> 
    <thead> 
      <tr id="header_row"> 
        <th class="quantity_th">Quantity</th> 
        <th class="left details_th">Details</th> 
        <th class="unitprice_th">Unit Price (£)</th> 
        <th class="salestax_th">VAT</th> 
        <th class="subtotal_th">Net Subtotal (£)</th> 
      </tr> 
    </thead> 
    <tfoot> 
      <tr id="discount_tr"> 
        <td colspan="2">&nbsp;</td> 
        <td colspan="2" class="item_r">10% Discount</td> 
        <td class="item_r">£250.00</td> 
      </tr> 
      <tr id="net_total_tr"> 
        <td colspan="2">&nbsp;</td> 
        <td colspan="2" class="item_r">Net Total</td> 
        <td class="item_r">2250.00</td> 
      </tr> 
      <tr id="vat_tr"> 
        <td colspan="2">&nbsp;</td> 
        <td colspan="2" class="item_r">VAT</td> 
        <td class="item_r">393.75</td> 
      </tr> 
      <tr id="total_tr"> 
        <td colspan="2">&nbsp;</td> 
        <td colspan="2" class="total" id="total_currency"><span class="currency">GBP </span> Total</td> 
        <td class="total">£2643.75</td> 
      </tr> 
    </tfoot> 
    <tbody> 
      <tr class="item odd"> 
        <td class="item_l">1 Day</td> 
        <td class="item_l">Details of project activity to be billed </td> 
        <td class="item_r">500.00</td> 
        <td class="item_r">17.5%</td> 
        <td class="item_r">500.00</td> 
      </tr> 
      <tr class="item"> 
        <td class="item_l">2 Days</td> 
        <td class="item_l">Other Details of project activity to be billed </td> 
        <td class="item_r">1000.00</td> 
        <td class="item_r">17.5%</td> 
        <td class="item_r">1000.00</td> 
      </tr> 
      <tr class="item odd"> 
        <td class="item_l">2 Days</td> 
        <td class="item_l">More d etails of project activity to be billed </td> 
        <td class="item_r">1500.00</td> 
        <td class="item_r">17.5%</td> 
        <td class="item_r">1000.00</td> 
      </tr> 
    </tbody> 
  </table> 
  <!-- invoice-amount --> 
  <div id="invoice-other"> 
    <h2>Other Information</h2> 
    <div id="company-reg-number"><strong>Company Registration Number:</strong> 9273109</div>  
    <div id="contract-number"><strong>Contract/PO:</strong> PO 87227643</div> 
  </div> 
  <!-- invoice-other --> 
  <div id="payment-details"> 
    <h2>Payment Details</h2> 
    <div id="bank_name">Bank Name</div> 
    <div id="sort-code"><strong>Bank/Sort Code:</strong> 32-75-97</div> 
    <div id="account-number"><strong>Account Number:</strong> 28270761</div> 
    <div id="iban"><strong>IBAN:</strong> 973547</div> 
    <div id="bic"><strong>BIC:</strong> 220197</div> 
    <div id="payment-reference"><strong>Payment Reference:</strong> INV001</div> 
  </div> 
  <!-- payment-details --> 
  <div id="comments">Payment should be made by bank transfer or cheque made payable to John Smith.</div> 
  <!-- comments --> 
</div>

我的问题是:如何将&lt;div id='invoice'&gt;bla bla bla&lt;/div&gt; 内的HTML 代码only 元素或只是该页面(ex: localhost/invoice/index.php) 的指定网址转换为可下载的pdf

我一直在搜索许多 pdf 类库,但找不到解决方案。

谁能给我一个解决方案、想法或可能的方法?

【问题讨论】:

  • 大多数 HTML 到 PDF 转换器不能针对特定的文档片段。选项: 1) 使用特定于打印的样式表来隐藏其他 DIV; 2)编写一些代码来解析文档并拉出所需的DIV; 3)用JS抓取想要的DIV,发送到PDF渲染脚本。

标签: php pdf-generation dompdf


【解决方案1】:

您可以使用此服务http://pdfmyurl.com/ 为网址下载 PDF,例如以这种格式 - http://pdfmyurl.com/?url=http://facebook.com

【讨论】:

  • 有离线(php类)转换器吗?
  • 查看另一个答案,我相信有几个选项,你可以很容易地在谷歌上找到它们。
【解决方案2】:

我会推荐使用Zend_Pdf,它可以提供一个用作PDF模板的页面,你可以做各种PDF的东西。

【讨论】:

  • 是的,它是 Zend 框架的一部分,它是开源的,因此是免费的。
【解决方案3】:

试试mPDF,它是一个很棒且易于使用的工具。你可以看看这个快速入门教程smaizys.com

【讨论】:

  • 不是问题的答案
【解决方案4】:

试试 TCPDF http://www.tcpdf.org/ 。它是一个用于生成 pdf 的 php 类。阅读http://www.tcpdf.org/examples/example_006.phps

【讨论】:

  • 我只能使用这个将特定的
    内容转换为 pdf 吗??
  • 我已经检查了文档,但我找不到关于将我的特定 div 仅转换为 pdf 的信息。你能给我确切的链接吗??
  • 您阅读过tcpdf.org/examples/example_006.phps 吗?如果这不符合您的要求,请查看其他示例。
【解决方案5】:

我建议使用 DOMPDF。

[https://github.com/dompdf/dompdf][1]

桌子有很大的问题。我分解了我的大型嵌套表并且它有所帮助(在它消耗高达 128M 的内存然后死亡之前——这是我在 php.ini 中的内存限制)但它使表完全混乱并且似乎没有得到图片。这些表格只是一些基本的东西,带有一些边框样式,可以在各个点添加一些线条;

【讨论】:

    猜你喜欢
    相关资源
    最近更新 更多
    热门标签