Form does not direct to another page when using MPDF

Asked

Viewed 322 times

0

I have a form that when sent, executes an action that directs to another PHP page, where several scripts are executed and at the end is generated a PDF file to download.

All scripts and PDF generation are executed correctly, but instead of directing to the page referenced in action, which in addition to executing scripts, also has the function of showing a message of success or error, the system continues on the form screen, passing the impression to the user that the form was not sent, possibly leading to duplicated records.

Form start and end:

<form id="form" method="post" action="function.php" enctype="multipart/form-data">
<input type="submit" name="submit" class="button" value="Enviar">

PHP code for PDF generation:

header('Pragma: no-cache');
ini_set('max_execution_time', 300);
$mpdf=new mPDF(); 
$mpdf->SetDisplayMode('fullpage');
$html = mb_convert_encoding($html, 'UTF-8', 'UTF-8');
$stylesheet = file_get_contents('_css/pdf_style.css');
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);
$mpdf->Output('myPDF.pdf', 'D');
$mpdf->charset_in='windows-1252';

Demonstration of what happens after sending the form: inserir a descrição da imagem aqui

I’ve checked the MPDF documentation, but I couldn’t find a function to solve this problem. Would this be a normal MPDF behavior or is missing some MPDF or PHP configuration?

1 answer

1


Let’s see, even though your HTML form has 2 buttons submit should work.

Your PHP is correct, what I understand is that PHP will change the header of Function.php for MIME-TYPE by the API itself, something like this:

header("Content-type:application/pdf")

Then by default the browser downloads the PDF and the page does not follow because something prevents you from opening the PDF directly or your browser is programmed to download automatically.

To solve this you can only to pass the information to the funciton.php and this page redirect with GET variables to another that manages download the PDF, for example dlpdf.php in get method or same POST method with AJAX.

Anyway do the test with this example, to see if it opens directly.

<?php 
 include("mpdf60/mpdf.php");
 $html = "
 <fieldset>
 <h1>Recibo de Pagamento</h1>
 <p class='center sub-titulo'>
 Nº <strong>0001</strong> - 
 VALOR <strong>R$ 700,00</strong>
 </p>
 <p>Recebi(emos) de <strong>Ebrahim Paula Leite</strong></p>
 <p>a quantia de <strong>Setecentos Reais</strong></p>
 <p>Correspondente a <strong>Serviços prestados ..<strong></p>
 <p>e para clareza firmo(amos) o presente.</p>
 <p class='direita'>Itapeva, 11 de Julho de 2017</p>
 <p>Assinatura ......................................................................................................................................</p>
 <p>Nome <strong>Alberto Nascimento Junior</strong> CPF/CNPJ: <strong>222.222.222-02</strong></p>
 <p>Endereço <strong>Rua Doutor Pinheiro, 144 - Centro, Itapeva - São Paulo</strong></p>
 </fieldset>
 <div class='creditos'>
 <p><a href='https://www.webcreative.com.br/artigo/gerar-pdf-com-php-e-html-usando-a-biblioteca-mpdf' target='_blank'>Aprenda como gerar PDF com PHP e HTML usando a biblioteca MPDF aqui</a></p>
 </div>
 ";

 $mpdf=new mPDF(); 
 $mpdf->SetDisplayMode('fullpage');
 $css = file_get_contents("css/estilo.css");
 $mpdf->WriteHTML($css,1);
 $mpdf->WriteHTML($html);
 $mpdf->Output();
 exit;
?>

And if it doesn’t work try using this system: DOMPDF

Browser other questions tagged

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