【问题标题】:PHP PDFLIB PHPMAILER Create, Save, SendPHP PDFLIB PHPMAILER 创建、保存、发送
【发布时间】:2017-06-24 08:46:21
【问题描述】:

我想使用 PDFLIB 创建一个 PDF,将其保存到服务器,然后发送给收件人并打开它! 使用此代码,我可以创建它,将其保存到服务器,发送但不在浏览器的 pdf 应用程序中显示。 有什么帮助吗?提前致谢。

<?php

    try {
     $p = new PDFlib();

     /* all strings are expected as utf8 */
     $p->set_option("stringformat=utf8");

     /*  open new PDF file; insert a file name to create the PDF on disk */
     if ($p->begin_document("test.pdf", "") == 0) {
     die("Error: " . $p->get_errmsg());
    }

    $p->set_info("Creator", "hello.php");
    $p->set_info("Author", "Rainer Schaaf");
    $p->set_info("Title", "Hello world (PHP)!");

    $p->begin_page_ext(595, 842, "");

    $font = $p->load_font("Helvetica-Bold", "unicode", "");
    if ($font == 0) {
    die("Error: " . $p->get_errmsg());
    }

    $p->setfont($font, 24.0);
    $p->set_text_pos(50, 700);
    $p->show("Hello world!");
    $p->continue_text("(says PHP)");
    $p->end_page_ext("");
    $p->end_document("");

   //#########################################
   //
   //############# PHP MAILER ################

   echo (extension_loaded('openssl')?'SSL loaded':'SSL not loaded')."\n";

   date_default_timezone_set('Etc/UTC');
   require 'phpmailer/PHPMailerAutoload.php';

   //Create a new PHPMailer instance

   $mail = new PHPMailer;

   //Tell PHPMailer to use SMTP
   $mail->isSMTP();
   //Enable SMTP debugging
  // 0 = off (for production use)
  // 1 = client messages
  // 2 = client and server messages
  $mail->SMTPDebug = 2;

//Ask for HTML-friendly debug output
$mail->Debugoutput = 'html';

//Set the hostname of the mail server
$mail->Host = 'smtp.gmail.com';
// use
$mail->Host = 'tls://smtp.gmail.com';

// if your network does not support SMTP over IPv6

//Set the SMTP port number - 587 for authenticated TLS, a.k.a. RFC4409 SMTP submission
$mail->Port = 587;

//Set the encryption system to use - ssl (deprecated) or tls
$mail->SMTPSecure = 'tls';

//Whether to use SMTP authentication
$mail->SMTPAuth = true;

//Username to use for SMTP authentication - use full email address for gmail
$mail->Username = "xxxx.xxxx@gmail.com";

//Password to use for SMTP authentication
$mail->Password = "xxxxxxxxxx";

//Set who the message is to be sent from
$mail->setFrom('xxxxxxx@gmail.com', 'First Last');

//Set an alternative reply-to address
//$mail->addReplyTo('replyto@example.com', 'First Last');
//Set who the message is to be sent to
$mail->addAddress('xxxxxxxx@email.com', 'John Doe');

//Set the subject line
$mail->Subject = 'PHPMailer GMail SMTP test';

//Read an HTML message body from an external file, convert referenced images to embedded,
//convert HTML into a basic plain-text alternative body
$mail->Body = 'trying to send a pdf';

//Replace the plain text body with one created manually
//$mail->AltBody = 'This is a plain-text message body';
//Attach an image file
$mail->addAttachment('test.pdf');

//send the message, check for errors
if (!$mail->send()) {
   echo "Mailer Error: " . $mail->ErrorInfo;
} else {
    echo "Message sent!";

 $buf = $p->get_buffer();
    $len = strlen($buf);

    header("Content-type: application/pdf");
    header("Content-Length: $len");
    header("Content-Disposition: inline; filename=hello.pdf");
    print $buf;
}
}
catch (PDFlibException $e) {
    die("PDFlib exception occurred in hello sample:\n" .
    "[" . $e->get_errnum() . "] " . $e->get_apiname() . ": " .
    $e->get_errmsg() . "\n");
}
catch (Exception $e) {
    die($e);
}

$p = 0;
?> 

【问题讨论】:

    标签: php phpmailer sendmail pdflib


    【解决方案1】:

    基本的 HTTP 问题。您正在输出文本之前将其标识为 PDF 的标题,这将破坏它。我希望您从那些对header() 的调用中收到错误记录,称“标头已发送”。注释掉echo "Message sent!"; 行。

    【讨论】:

    • 感谢同步。我找到了另一种方法来解决我的问题。我不在应用程序中显示它。我将它下载到客户端并通过电子邮件发送,这对我来说已经足够了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-01
    • 2018-09-10
    • 2018-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多