2017-06-24 3 views
-1

Je voudrais créer un PDF en utilisant PDFLIB, l'enregistrer sur le serveur, l'envoyer au destinataire et l'ouvrir après tout! Avec ce code, je peux le créer, l'enregistrer sur le serveur, l'envoyer mais ne pas l'afficher dans l'application pdf du navigateur. Une aide à ce sujet? Merci d'avance.PHP PDFLIB PHPMAILER Créer, Enregistrer, Envoyer

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

Répondre

0

Problème HTTP de base. Vous produisez le texte avant les en-têtes qui l'identifient comme PDF, ce qui va le casser. Je m'attends à ce que vous obteniez des erreurs en disant "les en-têtes déjà envoyés" de ces appels à header(). Commentez la ligne echo "Message sent!";.

+0

Merci Synchro. J'ai trouvé une autre façon de résoudre mon problème. Je ne l'affiche pas dans l'application. Je le télécharge au client et l'envoie par email ce qui me suffit. – Pol