我正在尝试将png转换为pdf,然后将其作为附件发送。转换工作正常,但是当我尝试通过电子邮件发送生成的pdf时,它会被破坏。使用image.pdf
下面的代码(删除了一些信息)很好,但附件已损坏。我做错了什么?
<?php
require_once 'class.phpmailer.php';
$data = str_replace('data:image/png;base64,', '', $_POST['imgBase64']);
$data = str_replace(' ', '+', $data);
$img = new Imagick();
$img->readImageBlob(base64_decode($data));
$img->setImageFormat('pdf');
file_put_contents('image.pdf', $img->getImageBlob());
$pdf = base64_encode($img->getImageBlob());
$email = new PHPMailer();
$email->From = 'webmaster@***';
$email->FromName = '***';
$email->Subject = 'Subject';
$email->Body = "Sent from http://www.example.com.\n\nDo not reply.";
$email->AddAddress('***@***.com');
$email->addStringAttachment($pdf, 'form.pdf', 'base64', 'application/pdf');
if(!$email->Send())
trigger_error("Failed to send email. {$email->ErrorInfo}", E_USER_ERROR);
?>