Form does not point to another page when using MPDF

0

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

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

Start and end of form:

<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 submission of the form:

I already checked the MPDF documentation but found no function to solve this problem. Is this a normal behavior of MPDF or is it missing any MPDF or PHP configuration?

    
asked by anonymous 28.02.2018 / 15:49

1 answer

1

Let's see, even your HTML form having 2 submit buttons should work.

Your PHP is correct, which I understand is that PHP changes the function.php header to MIME-TYPE by the API itself, something like this:

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

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

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

Anyway, 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 does not work try using this system: DOMPDF

    
01.03.2018 / 02:18