Save PDF file to PHP temp

Asked

Viewed 1,548 times

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 in the same?

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

Some way of the stones?

  • Hello Gabriel, managed to use the answer? Something is missing?

  • Actually, I changed my company until

  • But at the time came to test? Think that your doubt may be the doubt of future visitors to the site.

  • No, the demand went to someone else I don’t know

  • Get out of the company fast

1 answer

5

You can use the stream called php://temp


Using MPDF

If you are using MPDF (version 7) you probably use Composer to install, then 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 like 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’re not using Poser and you’re 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 equal to the first example, it keeps so:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.