Doubt with pdf generation and sending via mpdf

Asked

Viewed 767 times

1

I have a question. I am generating a.pdf file from the mpdf library. After generating I saved on the server. Well, I would like to generate the pdf file and not save on the server and yes, send them to the client email. Even because the necessary data I saved them in the database. I picked up an example on the web, however, does not send.

The sending code.

<?php>

//recuperando os dados do cliente

$cliente = $_POST['cli_nome'];
$email_cli = $_POST['cli_email'];

$mpdf=new mPDF();

$mpdf->WriteHTML($pagina);  <<<<

$content = $mpdf->Output('', 'S');

$content = chunck_split(base64_encode($content));
$mailto = $email_cli;
$from_name = 'Comercial SC';
$from_mail = '[email protected]';
//$replyto = '[email protected]';
$uid = md5(uniqid(time()));
$subject = 'Seu Boleto';
$message = 'Olá '. $cliente . '! Obrigado pela preferência.';
$filename = $arquivo;

$header = "From: ".$from_name." <" .$from_mail.">\r\n";
$header .= "Reply-To: ".$replyto."\r\n";
$header .= "MIME-Version: 1.0\r\n";
$header .= "Content-Type: multipart/mixed: boundary=\"".$uid. "\"\r\n\r\n";
$header .= "This is a multi-part message in MIME format.\r\n";
$header .= "--".$uid. "\r\n";
$header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
$header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
$header .= $message."\r\n\r\n";
$header .=  "--" .$uid. "\r\n";
$header .= "Content-Type: application/pdf; name=\"".$filename."\"\r\n";
$header .= "Content-Transfer-Encoding: base64\r\n";
$header .= "Content-Disposition: attachment; filename=\"" .$filename."\"\r\n\r\n";
$header .= $content. "\r\n\r\n";
$header .= "--".$uid."--";
$is_sent = @mail($mailto, $subject, "", $header);

$mpdf->Output();
exit;

?>

You’re making a mistake here on this line:

$content = chunck_split(base64_encode($content));

Even, here I create the body of the file .pdf:

//Criando arquivo pdf...
include('pdf/mpdf.php');

$pagina = '  <<<<<
 <html> 
    (...)

2 answers

1


GENERATE PDF, ATTACH AND SEND BY E-MAIL: //Creating a pdf file... //Instantiating the class... include('pdf/mpdf.php');

$pagina = '
 <html> 
  <!DOCTYPE html>
  <head>
  <style type="text/css">
  .cab_boleto {
   font-size: 20px;
   font-family: Georgia, "Times New Roman", Times, serif;
   }
   table.bordasimples {border-collapse: collapse;
   }
   table.bordasimples tr td {border:1px solid #FF0000;
   }
  </style>
  <title></title>
  </head>

- BILLET FOR PAYMENT - FEDERAL SAVINGS BANK | 104-0 RECEIPT FROM THE BALCONY Place of deposit: Agencies of Caixa Econômica and Lotérica. Due date: '.$maturity.'

Beneficiary: Shalom Criates - MEI - CNPJ: XX.XXX.XXX/000X-XX

Ag./Op /Account: 00XX-0XX-000XXXX-X

Address of the beneficiary: Rua XXXX dos XXX, 000 - Bairro XXXXX XXX - Bhte - MG - Cep. XX.XXX-XXX Date of document: '.$today. ' Number of doc: '.$ndoc. ' Processing date: '.$today. ' Kind of document: >>>>>> R? <<<<<< Document value: R$'. $Tg.'

* * * MR. Cashier, do not receive after due. * * *

* * * Deposit related to purchase order in virtual shop. * * *

Depositor: '.$customer. ' Address: '.$address. ',' . $numero. ' - '.$comple. ' - '.$neighborhood. ' - '.$city. ' - '.$state. ' - Cep:'. $cep. ' CPF.: '.$Cpf. ' Request No: '.$numped. ' Obs.: Gentileza, after making deposit, send print of receipt with no order, date and time to Whatsapp 9-xxxxxxxx
  </body>
</html>
';

$numped= "SC". $requested; $file = $numped . ". pdf"; $mpdf = new mPDF(); $mpdf->Writehtml($page); $mpdf->Output('pdf/tmp/' . $file, 'F');

// I - Opens in browser // F - Save file to server // D - Save the file to the user’s computer

//Attaching the.pdf file and sending to the email informed by the customer...

//recovering the customer’s data... $client = $_POST['cli_name']; $email_cli = $_POST['cliemail'];

// Include class in your file require_once 'phpmailer/class.phpmailer.php'; require_once 'phpmailer/class.smtp.php';

//require_once('phpmailer/class.phpmailer.php'); //require_once('phpmailer/class.smtp.php');

//Instantiate the email class $mail = new Phpmailer(); $mail->Issmtp(); $mail->Ishtml(true); $mail->Charset = 'utf-8'; // Sets the message charset $mail->Smtpauth = true; // Allow§SMTP authentication $mail->Host = 'smtp.gmail.com'; // Defines the SMTP server $mail->Username = '[email protected]'; // SMTP user account $mail->Password = '********'; // SMTP password account
$mail->Smtpsecure = 'tls'; // Enable TLS Encryption, ssl also accepted $mail->Port = 587; // TCP port to connect to

// Realizando o envio do email

Try { // Sender $mail->Addreplyto('[email protected]', 'My Name'); $mail->Setfrom('[email protected]', 'My Name');

// Destinatário
$mail->AddAddress($email_cli, $cliente);

// Assunto
$mail->Subject = 'Boleto de Pagamento';

// Mensagem para clientes de email sem suporte a HTML
$mail->AltBody = 'Olá '.$cliente . " segue em anexo o boleto para o pagamento do seu pedido";

// Mensagem para clientes de email com suporte a HTML
$mail->MsgHTML('<p>Olá ' . $cliente . ' ! Obrigado pela preferência! <br> Segue em anexo o boleto para o pagamento.</p>');

// Adicionar anexo
$caminho = 'pdf/tmp/';
$ficheiro = $arquivo;

$mail->AddAttachment($caminho.$ficheiro);

// Enviar email
$mail->Send();

//echo "E-mail enviado com sucesso!";
echo('<script type="text/javascript">alert("O boleto foi enviado para o e-mail informado!")</script>');
session_destroy();
exit ($refresh);
}
catch (phpmailerException $e) {
// Mensagens de erro do PHPMailer
echo $e->errorMessage();
}
catch (Exception $e) {
// Outras mensagens de erro
echo $e->getMessage();

}

?>

//Note: I took this code on the web and adapted it for my case.

0

You can do it this way:

  • Save to server in a temporary folder
  • Send attached by email with your own email service
  • Finally, delete the file from the temporary folder on the server
  • How this operation is normally performed?

  • Which one? I do it in a similar way here in a system, only instead of sending by email, when generating . pdf, I allow download of the file and soon after delete from the temporary folder.

  • Right David, however I would like to generate the pdf file and then send it to the client email. Is it possible without storing it in temporary folder? Even if in case I open the file in the client browser?

  • I can’t tell you.

  • Ask me a question: on this line $content = $mpdf->Output(', ’S'); do I need to enter the name of the PDF file generated that way? $content = $mpdf->Output('$file', ’S');

  • No, it should be $content = $mpdf->Output($arquivo, 'S'); matching $file is a string, for example, 'arquivo.pdf'

  • Where to put a solution found as a form of collaboration in stackoverflow?

  • Post as answer to your own question :)

Show 4 more comments

Browser other questions tagged

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