Using TCPDF to Convert specific div to PDF

一曲冷凌霜 提交于 2019-12-25 06:25:57

问题


I have a web application written in javascript. At the end of the application, the user is presented with a receipt that shows all their actions during the course of their operating the application. I want convert this receipt into a PDF file so that it can be accessed more easily (e.g., download, print, etc.) I am having trouble targeting the specific div that I wish to use as the receipt.

<div id="receipt"> --- Some Content --- </div>

I want to target the specific div 'receipt' but only at the end of the application (I am modifying the DOM a lot, and thus the receipt div contains new content each time, and is only relevant at the end). What I am wondering is whether there is some PHP command to get the HTML text from a webpage so that I can input the variable into the TCPDF PHP call:

$pdf->writeHTML($html, true, false, true, false, '');

Where the first variable is some html text.

How can I declare a variable, $html, that contains a specific div from the .html page?


回答1:


  1. Add a 'print' button at the bottom of the receipt.
  2. Add a javascript function at the 'onclick' event of the print button. Get the content of the receipt div ( using jquery: $('#receipt').html() or javascript 'document.getEmementById('receipt').innerHTML'). Add this content to a hidden variable in a form. Submit the form to a dedicated php file. (<form target='_blank')
  3. You got the the post data in the dedicated php file.

    $html = $_POST['hiddenHtml'];

  4. $pdf->writeHTML($html, true, false, true, false, '');



回答2:


You could use PHP: DOM to search through the HTML file and find the contents of the DIV, and then convert what it finds into a PDF.



来源:https://stackoverflow.com/questions/14149153/using-tcpdf-to-convert-specific-div-to-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!