详情页

Centos7 下安装 wkhtmltopdf PHP调用wkhtmltopdf

时间:2023年10月14日

编辑:佚名

两种方式安装,其中方法二比较顺利
方法一
1. 安装依赖
yum install -y fontconfig libX11 libXext libXrender libjpeg libpng xorg-x11-fonts-75dpi xorg-x11-fonts-Type1
2. 下载 wkhtmltopdf
wget https://github.com/wkhtmltopdf/packaging/releases/download/0.12.6-1/wkhtmltox-0.12.6-1.centos7.x86_64.rpm
3. 解压 wkhtmltopdf
rpm -ivh wkhtmltox-0.12.6-1.centos7.x86_64.rpm
4. 查看 wkhtmltopdf
whereis wkhtmltopdf
方法二:在 laravel 项目中使用 composer 安装
1. composer 安装
composer require h4cc/wkhtmltopdf-amd64 0.12.xcomposer require h4cc/wkhtmltoimage-amd64 0.12.x
2. 接下来将安装好的 wkhtmltopdf 复制到 Linux 系统可执行命令的目录中
cp vendor/h4cc/wkhtmltoimage-amd64/bin/wkhtmltoimage-amd64 /usr/local/bin/cp vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64 /usr/local/bin///并使其可执行:chmod +x /usr/local/bin/wkhtmltoimage-amd64 chmod +x /usr/local/bin/wkhtmltopdf-amd64
可能存在的问题
1. 中文字体无法显示
// 下载 windows 宋体字体,将 simsunbd.ttf 文件导入到 centos 系统 /usr/share/fonts/chinese/TrueType 文件目录下
PHP代码
<br>
<?php
    //转成pdf
        $html=$_POST['html'];
        //Turn on output buffering
        ob_start();
        $html='
                        '.$html;
        //这儿可以引入生成的Html的样式表  路径可以是绝对路径也可以是相对路径,也可以把样式表文件复制到临时html文件的目录下 即这儿的demo文件目录下(默认) 也可以直接把样式写在html页面中直接传递过来
        //$html = ob_get_contents();
        //$html=$html1.$html;
        $filename = "hld";
        //save the html page in tmp folder  保存的html临时文件位置 可以是相对路径也是可以是绝对路径 下面用相对路径
        file_put_contents("{$filename}.html", $html);
        //Clean the output buffer and turn off output buffering
        ob_end_clean();
        //convert HTML to PDF
        shell_exec("wkhtmltopdf -q {$filename}.html {$filename}.pdf");
        if(file_exists("{$filename}.pdf")){
            header("Content-type:application/pdf");
            header("Content-Disposition:attachment;filename={$filename}.pdf");
            echo file_get_contents("{$filename}.pdf");
            //echo "{$filename}.pdf";
        }else{
            exit;
        }
     ?>
相关文章
猜你需要