Force download or show in another window a pdf using html2pdf

3

Can you please support me?

I am developing a simple system for a company, they want to generate service orders. It is a form with various inputs that the client fills in order to generate a PDF with all this information.

Using htmltopdf link The code is simple:

    <-- formulario con inputs id="generarOrden"-->

    // jquery
    /* Pruebas de formulario de orden de servicio */
        $("#generarOrden").off("submit").on("submit", function(e){
            e.preventDefault();
            var data = $(this).serialize();
            $.ajax({
                url : "procesos/ordenes.php" ,
                dataType : "json" ,
                type : "post" ,
                data : data ,
                success : function(rD){

                    /* Resultado correcto */
                    if (rD.status == 200) {
                        console.log("Petición correcta");
                    }

                    /* Error en la petición */
                    if (rD.status == 404) {
                        console.log("Algo está mal");
                    }
                }
            })
        });

//archivo procesos/ordenes.php (solo de pruebas)
if (isset($_POST)) {

    // Guardar los valores en variables de SESSION para su uso
    foreach ($_POST as $key => $value) {
        $_SESSION["ordenDeServicio"][$key] = $value;
    }

    // Tipo de orden de servicio
    $tipo = (isset($_GET["tipo"])) ? "blank" : "filled";
    $orden = new PDF();

    return json_encode(array("status" => 200 , "response" => $orden->generar_orden_de_servicio($_POST)));
    exit();

} else {

    http_response_code(400);
    return false;

}

Everything up there works correctly, it returns me in the callback the pdf as such:

I want to know if I can open that ajax response in another tab showing the pdf or force a download, but I can not get it.

    
asked by RobertOrozco 28.01.2018 в 07:31
source

1 answer

2

To generate the pdf as such (configuration of html2pdf ):

$contenido=ob_get_clean();
require_once('html2pdf/html2pdf.class.php');

try{    
    $html2pdf = new HTML2PDF('P','A4','es', false, 'UTF-8'); // hoja en vertical
    //$html2pdf = new HTML2PDF('L','A4','es', false, 'UTF-8'); // hoja en horizontal
    $html2pdf->setDefaultFont('Courier'); // Tipo de letra
    $html2pdf->writeHTML($contenido, isset($_GET['vuehtml'])); // Pdf
    $html2pdf->Output($titulo); // Titulo del pdf
}

catch(HTML2PDF_exception $e) { echo $e; }

To make html2pdf go directly to download the pdf instead of opening the pdf you use:

$path="C:/wamp/www/tusistema/archivos/";
$file="archivo.pdf";
$path=$path.$file;
header('Content-type: application/pdf');
//header('Content-Disposition: inline; filename="archivo.pdf"');
header('Content-Disposition: attachment; filename="archivo.pdf"');
readfile($path);

Note: The line //header('Content-Disposition: inline; filename="archivo.pdf"'); is to show the file in a new browser window, uncomment it when you want to achieve this.

Note 2: The line that manages to force the download without opening the document is: header('Content-Disposition: attachment; filename="archivo.pdf"');

    
answered by 28.01.2018 в 12:25