在电子邮件中附加使用 HTML div 创建的 PDF(Java)



>我需要利用HTMLdiv标签的内容并从中创建PDF,同时维护与内容关联的css。

我在后端使用 Java 发送电子邮件,因此我希望能够完整地附加此 PDF,并将其与电子邮件一起发送。

我真的需要一些帮助来解决这个问题。

> This sample code will help you an dgive u an idea.     <?php
>     /*
>     mPDF: Generate PDF from HTML/CSS (Complete Code)
>     */
>     require_once( 'mpdf/mpdf.php'); // Include mdpf
>     $stylesheet = file_get_contents('assets/css/pdf.css'); // Get css content
>     $html = '<div id="pdf-content">
>                   Your PDF Content goes here (Text/HTML)
>              </div>';
>     // Setup PDF
>     $mpdf = new mPDF('utf-8', 'A4-L'); // New PDF object with encoding & page size
>     $mpdf->setAutoTopMargin = 'stretch'; // Set pdf top margin to stretch to avoid content overlapping
>     $mpdf->setAutoBottomMargin = 'stretch'; // Set pdf bottom margin to stretch to avoid content overlapping
>     // PDF header content
>     $mpdf->SetHTMLHeader('<div class="pdf-header">
>                               <img class="left" src="assets/img/pdf_header.png"/>                      
>                           </div>'); 
>     // PDF footer content                      
>     $mpdf->SetHTMLFooter('<div class="pdf-footer">
>                             <a href="http://www.lubus.in">www.lubus.in</a>
>                           </div>'); 
>      
>     $mpdf->WriteHTML($stylesheet,1); // Writing style to pdf
>     $mpdf->WriteHTML($html); // Writing html to pdf
>     // FOR EMAIL
>     $content = $mpdf->Output('', 'S'); // Saving pdf to attach to email 
>     $content = chunk_split(base64_encode($content));
>     // Email settings
>     $mailto = $email;
>     $from_name = 'LUBUS PDF Test';
>     $from_mail = 'email@domain.com';
>     $replyto = 'email@domain.com';
>     $uid = md5(uniqid(time())); 
>     $subject = 'mdpf email with PDF';
>     $message = 'Download the attached pdf';
>     $filename = 'lubus_mpdf_demo.pdf';
>     $header = "From: ".$from_name." <".$from_mail.">rn";
>     $header .= "Reply-To: ".$replyto."rn";
>     $header .= "MIME-Version: 1.0rn";
>     $header .= "Content-Type: multipart/mixed; boundary="".$uid.""rnrn";
>     $header .= "This is a multi-part message in MIME format.rn";
>     $header .= "--".$uid."rn";
>     $header .= "Content-type:text/plain; charset=iso-8859-1rn";
>     $header .= "Content-Transfer-Encoding: 7bitrnrn";
>     $header .= $message."rnrn";
>     $header .= "--".$uid."rn";
>     $header .= "Content-Type: application/pdf; name="".$filename.""rn";
>     $header .= "Content-Transfer-Encoding: base64rn";
>     $header .= "Content-Disposition: attachment; filename="".$filename.""rnrn";
>     $header .= $content."rnrn";
>     $header .= "--".$uid."--";
>     $is_sent = @mail($mailto, $subject, "", $header);
>     //$mpdf->Output(); // For sending Output to browser
>     $mpdf->Output('lubus_mdpf_demo.pdf','D'); // For Download
>     exit;
>     ?>

最新更新