2017-06-24 1 views
-1

Ich möchte ein PDF mit PDFLIB erstellen, speichern Sie es auf dem Server, senden Sie es an den Empfänger und öffnen Sie es schließlich! Mit diesem Code kann ich es erstellen, auf dem Server speichern, senden, aber nicht in der PDF-Anwendung des Browsers anzeigen. Irgendwelche Hilfe dazu? Danke im Voraus.PHP PDFLIB PHPMAILER Erstellen, Speichern, Senden

<?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; 
?> 

Antwort

0

Grundlegende HTTP-Problem. Sie geben Text vor die Header aus, die es als eine PDF identifizieren, die es brechen wird. Ich erwarte, dass Sie Fehler erhalten, die protokolliert werden, dass "Header bereits gesendet" von diesen Anrufen an header() sind. Kommentieren Sie die echo "Message sent!"; Zeile.

+0

Danke Synchro. Ich habe einen anderen Weg gefunden, mein Problem zu lösen. Ich zeige es nicht in der Anwendung an. Ich lade es auf den Client herunter und sende es per E-Mail, was für mich genug ist. – Pol