Created
September 28, 2018 07:05
-
-
Save pdfcrowd/b4e9f6a1915b468ddcf0884c0f233c2b to your computer and use it in GitHub Desktop.
Generate a PDF using Pdfcrowd API v2 and send it as an email attachment (II.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'pdfcrowd.php'; | |
try | |
{ | |
// generate a PDF file | |
$client = new \Pdfcrowd\HtmlToPdfClient($username, $apikey); | |
$pdf = $client->convertString('Hello World'); | |
//Deal with the email | |
$to = 'recipient@example.com'; | |
$from = 'sender@example.com'; | |
$subject = 'a PDF file for you'; | |
$content = 'Please find the file attached'; | |
$attachment = chunk_split(base64_encode($pdf)); | |
$filename = "myfile.pdf"; | |
$boundary =md5(date('r', time())); | |
$headers = "From: $from\r\nReply-To: $from"; | |
$headers .= "\r\nMIME-Version: 1.0\r\nContent-Type: multipart/mixed; boundary=\"_1_$boundary\""; | |
$message="This is a multi-part message in MIME format. | |
--_1_$boundary | |
Content-Type: multipart/alternative; boundary=\"_2_$boundary\" | |
--_2_$boundary | |
Content-Type: text/plain; charset=\"iso-8859-1\" | |
Content-Transfer-Encoding: 7bit | |
$content | |
--_2_$boundary-- | |
--_1_$boundary | |
Content-Type: application/pdf; name=\"$filename\" | |
Content-Transfer-Encoding: base64 | |
Content-Disposition: attachment | |
$attachment | |
--_1_$boundary--"; | |
mail($to, $subject, $message, $headers); | |
} | |
catch(PdfcrowdException $e) | |
{ | |
echo "Pdfcrowd Error: " . $e->getMessage(); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment