如何设置PHP wkhtmltopdf

 首先参考:Composer三步曲:安装、使用、发布

在 php 路径下,应能打开命令行输入php -v能够看到php版本信息。

然后执行以下三条:

php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');"

php composer-setup.php

php -r "unlink('composer-setup.php');"

这时遇到报错:

Unable to find the wrapper "https" - did you forget to enable it when you configured PHP?

参考:https://www.cnblogs.com/ec04/p/12390050.html

1.windows下的PHP,只需要到php.ini中把extension=php_openssl.dll前面的;删掉,重启服务就可以了。

经查,我的 php 没有这一项,添加,然后 iisreset 。再次执行以上三条,通过,下载安装成功。

All settings correct for using Composer
Downloading...

Composer (version 2.7.7) successfully installed to: C:\php\composer.phar
Use it: php composer.phar

C:\php>php comoser.phar
Could not open input file: comoser.phar

C:\php>php composer.phar
   ______
  / ____/___  ____ ___  ____  ____  ________  _____
 / /   / __ \/ __ `__ \/ __ \/ __ \/ ___/ _ \/ ___/
/ /___/ /_/ / / / / / / /_/ / /_/ (__  )  __/ /
\____/\____/_/ /_/ /_/ .___/\____/____/\___/_/
                    /_/
Composer version 2.7.7 2024-06-10 22:11:12

最后参考:php使用wkhtmltopdf html转为ptf或图片_php wkhtmlto-CSDN博客

执行:composer require mikehaertl/phpwkhtmltopdf

这时有问题,需要改为:php composer.phar require mikehaertl/phpwkhtmltopdf

执行通过:

./composer.json has been created
Running composer update mikehaertl/phpwkhtmltopdf
Loading composer repositories with package information
Updating dependencies
Lock file operations: 3 installs, 0 updates, 0 removals
  - Locking mikehaertl/php-shellcommand (1.7.0)
  - Locking mikehaertl/php-tmpfile (1.2.1)
  - Locking mikehaertl/phpwkhtmltopdf (2.5.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 3 installs, 0 updates, 0 removals
  - Downloading mikehaertl/php-tmpfile (1.2.1)
  - Downloading mikehaertl/php-shellcommand (1.7.0)
  - Downloading mikehaertl/phpwkhtmltopdf (2.5.0)
 0/3 [>---------------------------]   0%
 2/3 [==================>---------]  66%
 3/3 [============================] 100%
  - Installing mikehaertl/php-tmpfile (1.2.1): Extracting archive
  - Installing mikehaertl/php-shellcommand (1.7.0): Extracting archive
  - Installing mikehaertl/phpwkhtmltopdf (2.5.0): Extracting archive
 0/3 [>---------------------------]   0%
 3/3 [============================] 100%
Generating autoload files
No security vulnerability advisories found.
Using version ^2.5 for mikehaertl/phpwkhtmltopdf

最后是如何使用:

(这里要吐槽一下:网上的示例为何都不讲 require_once('...') 呢?)

//定位文件位置
require_once('C:\php\vendor\mikehaertl\php-tmpfile\src\File.php');
require_once('C:\php\vendor\mikehaertl\php-shellcommand\src\Command.php');
require_once('C:\php\vendor\mikehaertl\phpwkhtmltopdf\src\Command.php');
require_once('C:\php\vendor\mikehaertl\phpwkhtmltopdf\src\Image.php');
require_once('C:\php\vendor\mikehaertl\phpwkhtmltopdf\src\Pdf.php');
//生成图片
use mikehaertl\wkhtmlto\Image;
//生成PDF
use mikehaertl\wkhtmlto\Pdf;

//生成HTML和CSS
$html = "
    <!DOCTYPE html>
    <html lang=\"en\">
    <head>
        <meta charset=\"UTF-8\">
        <title>Title</title>
    </head>
    <style>
        table {
            border-collapse: collapse;
            width: 100%;
            max-width: 600px;
            margin: 0 auto;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 10px;
            text-align: center;
        }
        th {
            background-color:#f2f2f2;
        }
    </style>
    <body>
    <table>
        <tr>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
        </tr>
        <tr>
            <td>张三</td>
            <td>20</td>
            <td>男</td>
        </tr>
        <tr>
            <td>李四</td>
            <td>25</td>
            <td>女</td>
        </tr>
    </table>
    </body>
    </html>";
//$html可以是文件路径也可以直接放html
$image = new Image($html);
// 懒得设置环境变量,直接把wkhtmltopdf安装路径写到这里就好
$image->binary = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltoimage';
$image->saveAs('test.png');

$pdf = new Pdf($html);
// 懒得设置环境变量,直接把wkhtmltopdf安装路径写到这里就好
$pdf->binary = 'C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf';
$pdf->saveAs('test.pdf');

相关推荐

  1. Flutter 如何设置状态栏

    2024-07-10 23:54:05       35 阅读
  2. 如何设置PHP wkhtmltopdf

    2024-07-10 23:54:05       6 阅读
  3. HTML如何设置字体样式?

    2024-07-10 23:54:05       13 阅读
  4. 如何设置redis集群

    2024-07-10 23:54:05       19 阅读

最近更新

  1. 网络安全相关竞赛比赛

    2024-07-10 23:54:05       0 阅读
  2. Open3D点云配准介绍-点云之间进行配准

    2024-07-10 23:54:05       0 阅读
  3. windows关闭双击过后的jar 包

    2024-07-10 23:54:05       0 阅读
  4. windows下flutter国内镜像恢复成外网链接

    2024-07-10 23:54:05       0 阅读
  5. Amazon Bedrock 常用场景及 Python 实现

    2024-07-10 23:54:05       0 阅读
  6. go语言web框架:Echo超详细一文搞透

    2024-07-10 23:54:05       0 阅读

热门阅读

  1. yolov5中训练长条型目标召回率低问题

    2024-07-10 23:54:05       6 阅读
  2. openssl error:0A000126:SSL routines:ssl3_read_n:unexpected eof

    2024-07-10 23:54:05       6 阅读
  3. 各种软件启动方式

    2024-07-10 23:54:05       6 阅读
  4. 【创作纪念日】我的三周年创作纪念日

    2024-07-10 23:54:05       6 阅读
  5. Github 2024-07-07 开源项目日报 Top10

    2024-07-10 23:54:05       8 阅读
  6. 部署LVS-DR群集

    2024-07-10 23:54:05       5 阅读
  7. python的列表推导式

    2024-07-10 23:54:05       7 阅读