Asked  8 Months ago    Answers:  5   Viewed   81 times

What is the easiest way to attach a PDF to an email via DOMPDF?

The end of my script I am using (part of it) is below:

$dompdf = new DOMPDF();
$dompdf->load_html($html);
$dompdf->render();    
//below to save the pdf file - not needed if emailing pdf
file_put_contents('/home/ststrave/public_html/pdf/STS_Brochure.pdf', $dompdf->output());
//below to open pdf in browser - required
$dompdf->stream("STS_Brochure_".rand(10,1000).".pdf", array("Attachment" => false));
jexit();

Just for clarification - this is being used in Joomla.

Appreciate the simplest/quickest way using standard PHP mail function.

Cheers ;-)

 Answers

20

PHP's mail function has no "standard" file attachment method. It's an extremely barebones interface to the SMTP system that forces you to do ALL the work of attaching a file yourself.

I strongly suggest using PHPMailer or Swiftmailer to do the email for you - it reduces the heavy grunt work of generating your own MIME email and inserting the attachment (many many lines of code) does to maybe 5 lines total.

Note that neither of them will handle a streamed PDF from DOMPDF. You'll have to save the PDF to a temporary file and attach that,

Wednesday, March 31, 2021
 
Fishingfon
answered 8 Months ago
97

You can do one thing like below which i am doing for my application. First create a folder in your server under the root directory for example. Then change read write permissions to that folder using chmod command.

Then get all the code in $html string.

$dompdf->load_html($html);    
$dompdf->render();
$pdf = $dompdf->output();
$file_location = $_SERVER['DOCUMENT_ROOT']."app_folder_name/pdfReports/".$pdf_name.".pdf";
file_put_contents($file_location,$pdf); 

Where pdfReports is the folder which i created to save all the pdf's. You can change to your folder name.

Saturday, May 29, 2021
 
jeremyharris
answered 5 Months ago
87

Create a MFMailComposeViewController and call addAttachmentData:mimeType:fileName:. The data will be the PDF you created. The mimeType will be application/pdf. And the fileName will be the name of the file in the email attachment. The code might look like something below:

From the tutorial you'll need to render your PDF into a NSMutableData object:

NSMutableData *pdfData = [NSMutableData data];
UIGraphicsBeginPDFContextToData(pdfData, bounds, nil);

Then at some point in the future you'll need to pass that pdfData to the MFMailComposeViewController.

MFMailComposeViewController *vc = [[[MFMailComposeViewController alloc] init] autorelease];
[vc setSubject:@"my pdf"];
[vc addAttachmentData:pdfData mimeType:@"application/pdf" fileName:@"SomeFile.pdf"];
Monday, August 2, 2021
 
Zword
answered 3 Months ago
61

Following helped me like charm, at least localy, and even with

def("DOMPDF_ENABLE_REMOTE", false);

The solution is to change the image SRC to the absolute path on the server, like this:

<img src="/var/www/domain/images/myimage.jpg" />

All of the following worked for me:

<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'/placeholder.jpg';?>"/>
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'placeholder.jpg';?>"/>
<img src="<?php echo $_SERVER["DOCUMENT_ROOT"].'./placeholder.jpg';?>"/>

$_SERVER["DOCUMENT_ROOT"] is C:/wamp/www/ZendSkeletonApplication/public

Thanks to this: lost in code

Monday, August 2, 2021
 
ericstumper
answered 3 Months ago
78

This is not a matter of your css or html. Basically this comes down to dompdf rendering your your svg differently then the browser. dompdf is rendering each group/item within the svg xml individually into the displayed image, whereas the browser is defining each group/item as part as the whole image before rendering.

Your best options here are:

  1. using PNG's instead of SVG's
    • this would definitely be the quickest solution if you're dealing with a small number of images, but if you're dealing with many images or auto-generated images this could become time consuming.
  2. altering your SVG XML to fit fit the way dompdf is rendering.
    • this could solve your issue with dompdf rendering, but could lead to issues with browser rendering, and could also be time consuming based on number of images and source.
  3. Opening an issue with dompdf
    • Probably the best long term solution, but the turn around time is unknown.
  4. Alter the dompdf source code to solve your issue
Tuesday, August 17, 2021
 
Byron Wall
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share