问题
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:
- Add a 'print' button at the bottom of the receipt.
- 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')
You got the the post data in the dedicated php file.
$html = $_POST['hiddenHtml'];
- $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