【问题标题】:Creating a PDF and sending by email创建 PDF 并通过电子邮件发送
【发布时间】:2011-08-20 00:06:48
【问题描述】:

我正在研究用户在表单中输入一些信息并通过mail() 发送的能力。

我希望将详细信息作为 PDF 附件发送。可能带有发件人的姓名和日期/时间。 test_user_06052011.pdf

我有一个 PDF 设计,但我不确定如何集成这个设计以在 PHP 中创建一个 PDf。

有没有人有我可以做到这一点的例子或方法?

【问题讨论】:

  • 欢迎来到 SO,sipher_z!请停止在问题标题中写标签。您已经完成了 11 个问题中的大部分问题。 >.
  • @Tomalak Geret'kal 为标题中的标签道歉。我会确保我以后不会这样做

标签: php pdf-generation


【解决方案1】:

老问题,但仍然相关,因此将其发布在此处,以使该问题的新访问者受益。

PDFMark 提供 API 来生成 PDF,也可以通过电子邮件发送。您可以从 HTML 创建 PDF 并指定一个电子邮件地址,生成的 PDF 将作为附件发送到该地址。

披露:我是开发者

【讨论】:

    【解决方案2】:

    这是完整的代码http://codexhelp.blogspot.in/2017/04/php-email-create-pdf-and-send-with.html

        /**/
        $mailto = $_POST['mailto'];
        $mailfrom = $_POST['mailfrom'];
        $mailsubject = $_POST['mailsubject'];
        $firstname = $_POST['firstname'];
        $lastname = $_POST['lastname'];
        $description = $_POST['description'];
    
    
        $description = wordwrap($description, 100, "<br />");
        /* break description content every after 100 character. */
    
    
        $content = '';
    
        $content .= '
    <style>
    table {
    border-collapse: collapse;
    }
    
     table{
     width:800px;
     margin:0 auto;
    }
    
     td{
    border: 1px solid #e2e2e2;
    padding: 10px; 
    max-width:520px;
    word-wrap: break-word;
    }
    
    
     </style>
    
     ';
        /* you css */
    
    
    
        $content .= '<table>';
    
        $content .= '<tr><td>Mail To</td> <td>' . $mailto . '</td> </tr>';
        $content .= '<tr><td>Mail From</td>   <td>' . $mailfrom . '</td> </tr>';
        $content .= '<tr><td>Mail Subject</td>   <td>' . $mailsubject . '</td> </tr>';
        $content .= '<tr><td>Firstname</td>   <td>' . $firstname . '</td> </tr>';
        $content .= '<tr><td>Lastname</td>   <td>' . $lastname . '</td> </tr>';
        $content .= '<tr><td>Description</td>   <td>' . $description . '</td> </tr>';
    
        $content .= '</table>';
    
    
        require_once('html2pdf/html2pdf.class.php');
    
    
        $to = $mailto;
        $from = $mailfrom;
        $subject = $mailsubject;  
    
        $html2pdf = new HTML2PDF('P', 'A4', 'fr');
    
        $html2pdf->setDefaultFont('Arial');
        $html2pdf->writeHTML($content, isset($_GET['vuehtml']));
    
        $html2pdf = new HTML2PDF('P', 'A4', 'fr');
        $html2pdf->WriteHTML($content);
    
    
        $message = "<p>Please see the attachment.</p>";
        $separator = md5(time());
        $eol = PHP_EOL;
        $filename = "pdf-document.pdf";
        $pdfdoc = $html2pdf->Output('', 'S');
        $attachment = chunk_split(base64_encode($pdfdoc));
    
    
    
    
        $headers = "From: " . $from . $eol;
        $headers .= "MIME-Version: 1.0" . $eol;
        $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol . $eol;
    
        $body = '';
    
        $body .= "Content-Transfer-Encoding: 7bit" . $eol;
        $body .= "This is a MIME encoded message." . $eol; //had one more .$eol
    
    
        $body .= "--" . $separator . $eol;
        $body .= "Content-Type: text/html; charset=\"iso-8859-1\"" . $eol;
        $body .= "Content-Transfer-Encoding: 8bit" . $eol . $eol;
        $body .= $message . $eol;
    
    
        $body .= "--" . $separator . $eol;
        $body .= "Content-Type: application/octet-stream; name=\"" . $filename . "\"" . $eol;
        $body .= "Content-Transfer-Encoding: base64" . $eol;
        $body .= "Content-Disposition: attachment" . $eol . $eol;
        $body .= $attachment . $eol;
        $body .= "--" . $separator . "--";
    
        if (mail($to, $subject, $body, $headers)) {
    
            $msgsuccess = 'Mail Send Successfully';
        } else {
    
            $msgerror = 'Main not send';
        }
    

    【讨论】:

    • 太棒了,我已经尝试过这个链接演示,它对我来说很好用,谢谢
    【解决方案3】:

    今天找不到使用原生 mail() 函数的理由。在大多数琐碎的情况下,我们可以使用 PHPMailer 库,它以 OOP 风格让我们有机会在不了解邮件头的情况下发送电子邮件。 解决方案甚至不保存物理文件

    $mail = new PHPMailer();
    ...
    $doc = $pdf->Output('S');
    $mail->AddStringAttachment($doc, 'doc.pdf', 'base64', 'application/pdf');
    $mail->Send();
    

    【讨论】:

      【解决方案4】:

      如果您使用 Acrobat 制作了可填充的 PDF,这里有很多代码可以帮助您入门。此代码需要最新版本的 phpmailer 才能工作,因此只需下载该代码并将其放在放置此代码的同一目录中的类文件夹中。让您的 pdf 表单提交到包含此代码的页面。

      /*  Branden Sueper 2012
      //  PDF to Email - PHP 5
      //  Includes: PHPMailer 5.2.1
      */
      <?php
      if(!isset($HTTP_RAW_POST_DATA)) {
          echo "The Application could not be sent. Please save the PDF and email it manually.";
          exit;
      }
      echo "<html><head></head><body><img src='loading.gif'>";
      
      //Create PDF file with data
      $semi_rand = md5(time());
      $pdf = $HTTP_RAW_POST_DATA;
      
       $file = $semi_rand . ".pdf"; 
       $handle = fopen($file, 'w+');
       fwrite($handle, $pdf);   
       fclose($handle);
      //
      
      require_once('class/class.phpmailer.php');
      //include("class.smtp.php"); // optional, gets called from within class.phpmailer.php if not already loaded
      
      $mail = new PHPMailer(false); // the true param means it will throw exceptions on errors, which we need to catch
      
      $mail->IsSMTP(); // telling the class to use SMTP
      
      try {
        $mail->Host       = "mail.xxxxxxx.com"; // SMTP server
        $mail->SMTPDebug  = 2;                     // enables SMTP debug information (for testing)
        $mail->SMTPAuth   = true;                  // enable SMTP authentication
        $mail->SMTPSecure = "ssl";                 // sets the prefix to the servier
        $mail->Host       = "smtp.gmail.com";      // sets GMAIL as the SMTP server
        $mail->Port       = 465;                   // set the SMTP port for the GMAIL server
        $mail->Username   = "xxxxxxx@xxxxxxx.com"; // GMAIL username
        $mail->Password   = "xxxxxxxx";            // GMAIL password
        $mail->AddAddress('Recipient@xxxxxxx.com', 'First Last');
        $mail->SetFrom('reply-to-address@xxxxxxx.com', 'First Last');
        $mail->Subject = 'Your Subject';
        $mail->Body = 'Hello!';
        $mail->AddAttachment($file); // attachment
        $mail->Send();
      
        //Delete the temp pdf file then redirect to the success page
        unlink($file);
        echo '<META HTTP-EQUIV="Refresh" Content="0; URL="success.php">';    
        exit;    
      } catch (phpmailerException $e) {
        //you can either report the errors here or redirect them to an error page
        //using the above META tag
        echo $e->errorMessage(); //Pretty error messages from PHPMailer
      } catch (Exception $e) {
        echo $e->getMessage(); //Boring error messages from anything else!
      }
        //Verify the temporary pdf file got deleted
        unlink($file);
      ?>
      

      PHPMailer 只需下载 PHP mailer 并根据自己的喜好调整上述代码。有关如何创建可填写 PDF 的更多信息,请访问 http://www.adobe.com/products/acrobatpro/create-fillable-pdf-forms.html

      祝你好运!我记得我花了 3 天多的时间试图找出一个非常相似的问题!

      【讨论】:

        【解决方案5】:

        根据您的要求,您还可以查看TCPDF,我经常使用它来从 PHP 动态创建 PDF...它具有(有限的)内置 HTML 到 PDF 功能并且非常易于使用(只需查看示例)。还有另一个主要好处:它仍在积极开发中(对某些人来说可能有点过于活跃:p)。

        【讨论】:

          【解决方案6】:

          使用wkhtmltopdf 是创建PDF 服务器站点的非常简单的方法。但是,您需要对服务器进行 shell 访问才能设置它。

          要创建 PDF,您需要两个文件:一个是 PHP,它生成要转换为 PDF 的 HTML。假设这是 invoice.php:

          <?php
              $id = (int) $_GET['id'];
          ?>
          <h1>This is invoice <?= $id ?></h1>
          <p>some content...</p>
          

          另一个,它将获取发票并使用 wkhtmltopdf 将其转换为 PDF:

          <?php
          
          $tempPDF = tempnam( '/tmp', 'generated-invoice' );
          $url = 'http://yoursite.xx/invoice.php?id=123';
          
          exec( "wkhtmltopdf  $url  $tempPDF" );
          
          header('Content-Type: application/pdf');
          header('Content-Disposition: attachment; filename=invoice.pdf');
          
          echo file_get_contents( $tempPDF );
          unlink( $tempPDF );
          

          创建 PDF 文件后,您还可以通过以下方式发送带有附件的邮件:

          <?php
          
          $to = "abc@gmail.com";
          $subject = "mail with attachment";
          
          $att = file_get_contents( 'generated.pdf' );
          $att = base64_encode( $att );
          $att = chunk_split( $att );
          
          $BOUNDARY="anystring";
          
          $headers =<<<END
          From: Your Name <abc@gmail.com>
          Content-Type: multipart/mixed; boundary=$BOUNDARY
          END;
          
          $body =<<<END
          --$BOUNDARY
          Content-Type: text/plain
          
          See attached file!
          
          --$BOUNDARY
          Content-Type: application/pdf
          Content-Transfer-Encoding: base64
          Content-Disposition: attachment; filename="your-file.pdf"
          
          $att
          --$BOUNDARY--
          END;
          
          mail( $to, $subject, $body, $headers );
          

          【讨论】:

            【解决方案7】:

            查看 FPDF - http://www.fpdf.org/ - 它是免费的,是生成 PDF 的绝佳工具

            有一个由 PHP 推荐的 PDF 生成器,但是它非常昂贵,而且现在这个名字让我难以理解,但是我已经多次使用 FPDF 并取得了巨大的成功。

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 2018-09-19
              • 2011-04-01
              • 2011-06-07
              • 1970-01-01
              • 1970-01-01
              • 2018-03-31
              • 2015-06-21
              相关资源
              最近更新 更多