Save PDF file in PHP temp

0

Is there any class, such as mPDF or fPDF, that can send its output to a temporary file, or something like that, to be sent via email to it?

The demand is as follows: I need the email to be automatically attached, without the guy selecting files.

Some way from the stones?

    
asked by anonymous 15.12.2017 / 14:23

1 answer

5

You can use stream called php://temp

Using MPDF

If you are using MPDF (version 7) you probably use the composer to install, so it should look like this:

require_once __DIR__ . '/vendor/autoload.php';

Then an example with mpdf:

$mpdf = new \Mpdf\Mpdf();
$mpdf->WriteHTML('Stack Overflow');

$mpdf->Output('php://temp', \Mpdf\Output\Destination::FILE);

And in Phpmailer use this:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

$mail = new PHPMailer(true);
try {
    //Server settings
    $mail->SMTPDebug = 0;
    $mail->isSMTP();
    $mail->Host = 'smtp1.example.com;smtp2.example.com';  //Configure o seu SMTP
    $mail->SMTPAuth = true;
    $mail->Username = '[email protected]';                 // usuario
    $mail->Password = 'secret';                           // senha
    $mail->SMTPSecure = 'tls';                            // TLS se tiver criptografia
    $mail->Port = 587;                                    //Porta do SMTP

    //Destinatários
    $mail->setFrom('[email protected]');

    //ADICIONA O ANEXO AQUI
    $mail->addAttachment('php://temp', 'Arquivo.pdf'); 

    $mail->isHTML(true);
    $mail->Subject = 'ASSUNTO';
    $mail->Body    = 'Mensagem';
    $mail->AltBody = 'mensagem para clientes de email que não usam HTML';

    $mail->send();
    echo 'Mensagem enviada';
} catch (Exception $e) {
    echo 'Message could not be sent.';
    echo 'Mailer Error: ' . $mail->ErrorInfo;
}

Using FPDF

If you are not using composer and are using FPDF, first import what you need:

use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;

require 'path/to/PHPMailer/src/Exception.php';
require 'path/to/PHPMailer/src/PHPMailer.php';
require 'path/to/PHPMailer/src/SMTP.php';
require 'path/to/fpdf.php';

Then call the FDPF:

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial',  'B',16);
$pdf->Cell(40, 10, 'Stack Overflow!');
$pdf->Output('F', 'php://temp');

And in phpmailer is the same as the first example, if it keeps like this:

//ADICIONA O ANEXO AQUI
$mail->addAttachment('php://temp', 'Arquivo.pdf'); 
    
15.12.2017 / 14:36