2017-06-24 35 views
-1

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

<?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 = "[email protected]"; 

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

//Set who the message is to be sent from 
$mail->setFrom('[email protected]', 'First Last'); 

//Set an alternative reply-to address 
//$mail->addReplyTo('[email protected]', 'First Last'); 
//Set who the message is to be sent to 
$mail->addAddress('[email protected]', '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; 
?> 

回答

0

基本的HTTP问题。您在之前输出的文字标识为PDF的标题,会将其分解。我希望你会收到错误记录,说明这些调用的“头文件已经发送”到header()。注释掉echo "Message sent!";一行。

+0

感谢Synchro。我找到了解决我的问题的另一种方法。我不会在应用程序中显示它。我将它下载到客户端,并通过电子邮件发送,这对我来说已经足够了。 – Pol