How to create a pdf file from an HTML using php, and then save it on the server

前端 未结 2 913
时光说笑
时光说笑 2021-02-06 18:06

I have a project to save pages created dynamically with php and store them on the server.

I am planning to store a replica of the page as a pdf; with all their images, t

2条回答
  •  时光取名叫无心
    2021-02-06 18:13

    From forosdelweb

    We are going to create a class to make the convertion in convertToPDF.php

     'ejemplo' , 'pdfs/nuevo-ejemplo' 
                    if empty --> it will be random 
    
    $content  : content of the pdf 
    
    $body     : true or false. 
                    true  --> add; , ,  to $content 
                    false --> do not alter the $content 
    
    $style    : the path of the CSS. It could be empty 
                     To load the css --> needs $body = true; 
    
    $mode     : true or false. 
                    true  --> save the file on the server and then show it  
                    false --> ask where to save it  
    
    $paper_1  : size of the paper[*] 
    $paper_2  : style of the paper[*] 
    
        [*] To see more options:  
            --> http://code.google.com/p/dompdf/wiki/Usage#Invoking_dompdf_via_the_command_line 
    
    /*----------------------------------------------------------*/  
    
    require_once("dompdf/dompdf_config.inc.php"); 
    
    function doPDF($path='',$content='',$body=false,$style='',$mode=false,$paper_1='a4',$paper_2='portrait') 
    {     
        if( $body!=true and $body!=false ) $body=false; 
        if( $mode!=true and $mode!=false ) $mode=false; 
    
        if( $body == true ) 
        { 
            $content=' 
             
             
             
                 
             
            ' 
                .$content. 
            ' 
            '; 
        } 
    
        if( $content!='' ) 
        {         
            //Añadimos la extensión del archivo. Si está vacío el nombre lo creamos 
            $path!='' ? $path .='.pdf' : $path = crearNombre(10);   
    
            //Las opciones del papel del PDF. Si no existen se asignan las siguientes:[*] 
            if( $paper_1=='' ) $paper_1='a4'; 
            if( $paper_2=='' ) $paper_2='portrait'; 
    
            $dompdf =  new DOMPDF(); 
            $dompdf -> set_paper($paper_1,$paper_2); 
            $dompdf -> load_html(utf8_encode($content)); 
            //ini_set("memory_limit","32M"); //opcional  
            $dompdf -> render(); 
    
            //Creamos el pdf 
            if($mode==false) 
                $dompdf->stream($path); 
    
            //Lo guardamos en un directorio y lo mostramos 
            if($mode==true) 
                if( file_put_contents($path, $dompdf->output()) ) header('Location: '.$path); 
        } 
    } 
    
    function crearNombre($length) 
    { 
        if( ! isset($length) or ! is_numeric($length) ) $length=6; 
    
        $str  = "0123456789abcdefghijklmnopqrstuvwxyz"; 
        $path = ''; 
    
        for($i=1 ; $i<$length ; $i++) 
          $path .= $str{rand(0,strlen($str)-1)}; 
    
        return $path.'_'.date("d-m-Y_H-i-s").'.pdf';     
    } 
    

    The next step is to create a page in this case index.php

     Aquí pondriamos por ejemplo la consulta 
    $html=' 
     
    
    
    Nombre Tipo Imagen Comentario Unidades Precio unidad
    pensandoo icono iconito pensativo 3 10
    fiesta icono 3 iconito festejando 1 24
    silbando icono bombilla silbando 19 50
    no no no icono 2 negacion 5 1
    ' ?> ... pero no tiene css if ( isset($_POST['PDF_6']) ) doPDF('',$html,true,'style.css',true); if ( isset($_POST['PDF_7']) ) doPDF('pdfs/nuevo-ejemplo',$html,true,'style.css',true); //lo guardamos en la carpeta pdfs ?>

    PHP

    FOROSDELWEB

    Mostrar PDF sin CSS
    Mostrar PDF con CSS
    Mostrar PDF con CSS sin definir el nombre
    Mostrar PDF con CSS y cambiando el formato de la hoja
    Guardar y abrir PDF sin CSS
    Guardar y abrir PDF con CSS sin definir el nombre
    Guardar en otro directorio y abrir PDF con CSS

    Finally you can have the file for your style style.css

    body{
    font:12px Arial, Tahoma, Verdana, Helvetica, sans-serif;
    background-color:#BECEDC;
    color:#000;
    }
    
    a h1{
    font-size:35px; 
    color:#FFF;
    }
    
    h2{
    color:#FC0;
    font-size:15px; 
    }
    
    table{
    width:100%;
    height:auto;
    margin:10px 0 10px 0;
    border-collapse:collapse;
    text-align:center;
    background-color:#365985;
    color:#FFF;
    }
    
    table td,th{
    border:1px solid black;
    }
    
    table th{
    color:#FC0; 
    }
    
    .menu{
    background-color:#69C;
    color:#FFF;
    }
    
    .menu a{
    color:#FFF; 
    }
    

提交回复
热议问题