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

前端 未结 2 882
时光说笑
时光说笑 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:35

    I had this same issue recently and found fpdf to be too complicated when your HTML contains formatted text and images. I had my server admin install Webkit HTML to PDF. It works great.

    This package will parse the stylesheet, images, tables, and any other element in the HTML document as if you had opened a web browser using webkit and saved the web page as a PDF.

    http://code.google.com/p/wkhtmltopdf/

    /**
    * Use wkhtmltopdf to convert an HTML file to PDF
    *
    * @param    string          URL to web page
    * @param    string          Path to PDF file to be written
    *
    * @return   boolean         True if the command succeeded; false otherwise
    */
    function htmlToPdf($source, $dest)
    {
        // Build command
        $command = "/usr/bin/wkhtmltopdf $source $dest 2>&1";
        exec($command, $output);
    
        $s = sizeof($output) - 1;
    
        if (substr($output[$s], -4, 4) == 'Done')
        {
            return true;
        }
    
        return false;
    }
    

提交回复
热议问题