简体   繁体   中英

Save HTML as PDF and email using MPDF

I need to be able to send a PDF version of a PHP page's HTML (with CSS styles intact as some tags are set to display: none in a @media print{} clause) as an email.

I found mpdf which looks relatively easy and seems like a solid solution. The class offers the ability to email a PDF file on the fly. This is great.

My dilemma is as follows:

I want to send page_1.php 's contents to send_email.php as a url variable to use as the PDF content.

To store this variable I have tried:

ob_start(); // at the top of the page

    // THE DYNAMICALLY GENERATED PAGE CONTENT

$html = ob_get_contents(); // I put the page contents into a variable
ob_end_clean();

For some reason this does not let the page load in it's entirety. Only a few tags show up. The ob_get_contents(); and ob_end_clean(); are sitting before the end </html> tag. Is this a potential cause? I need to use these functions before the end of the page to utilize the variable higher up at the actual 'email link' I created.

I have also tried:

$page_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; // to grab the current url
$html = file_get_contents($page_url); // place contents in a variable

For some reason, this does not allow any page on the site to load. All one gets is a spinning wheel in the browser url input.

Even if I can get the page's content into a variable and parse it to send_email.php , how would I retain the CSS properties when creating the PDF?

Would a javascript alternative be more viable?

Any help would be outstanding!

I've had success posting the html to the php file:

$html = $_POST['html'];
$head.= '<link rel="stylesheet" type="text/css" href="/app/css/app.css" /> ';
$head.= '<link rel="stylesheet" type="text/css" href="/app/css/pdf_style.css" /> ';
require_once 'mpdf/mpdf.php';
$mpdf = new mPDF('', 'Letter', 0, '', 12.7, 12.7, 12.7, 12.7, 0, 0);
$mpdf->WriteHTML($head.$html,0);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM