使用MPDF在PHP中生成PDF

发布于 2025-01-19 22:02:23 字数 369 浏览 3 评论 0原文

我通过composer成功安装了mpdf。

这是我的 PHP 代码:

require_once __DIR__ . '/vendor/autoload.php';
$mpdf=new \Mpdf\Mpdf();
$mpdf->WriteHTML("<h1>test</h1>");
$file='abcd.pdf';
$mpdf->output($file,'I');

PDF 不会生成。我在 Chrome 和 Firefox 中尝试过。 autoload.php 的位置是正确的。 我也尝试了 D、F 和 S,作为 $mpdf->output 的第二个参数,但没有任何效果。

I installed mpdf successfully via composer.

Here is my PHP code:

require_once __DIR__ . '/vendor/autoload.php';
$mpdf=new \Mpdf\Mpdf();
$mpdf->WriteHTML("<h1>test</h1>");
$file='abcd.pdf';
$mpdf->output($file,'I');

PDF won't generate. I tried in Chrome and Firefox.
The location of autoload.php is correct.
I tried D, F and S as well, as the second argument for $mpdf->output, none worked.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

诗酒趁年少 2025-01-26 22:02:23

您的代码实际上有效,请仔细检查您的PHP版本
并请注意,MPDF仅与php≥5.6.0和&lt兼容; 7.4.0。

https://mpdf.github.io//大约是mpdf/unignts-v7.html

your code actually works, please double check your php version
and note that mpdf is compatible only with PHP ≥ 5.6.0 and < 7.4.0.

https://mpdf.github.io/about-mpdf/requirements-v7.html

无妨# 2025-01-26 22:02:23
require_once __DIR__ . '/vendor/autoload.php';
$mpdf=new \Mpdf\Mpdf();
$mpdf->WriteHTML("<h1>test</h1>");
$file='abcd.pdf';

$mpdf->OutputHttpDownload($file);

您可以使用传递文件名称的outputhttpdownload方法,它将起作用!

require_once __DIR__ . '/vendor/autoload.php';
$mpdf=new \Mpdf\Mpdf();
$mpdf->WriteHTML("<h1>test</h1>");
$file='abcd.pdf';

$mpdf->OutputHttpDownload($file);

You can use the OutputHttpDownload method passing the name of your file and it will work!

江湖正好 2025-01-26 22:02:23

使用此功能创建一个临时目录,然后执行此操作

$upload_dir = wp_upload_dir();

$custom_temp_dir = $upload_dir['basedir'] . '/mpdf_temp/';

if (!file_exists($custom_temp_dir)) {
    mkdir($custom_temp_dir, 0755, true);
}
$mpdf = new \Mpdf\Mpdf(['tempDir' => $custom_temp_dir]);
$mpdf->WriteHTML("<h1>test</h1>");
$file='abcd.pdf';
$mpdf->output($file,'F');

Use this create a temporary directory and then do this

$upload_dir = wp_upload_dir();

$custom_temp_dir = $upload_dir['basedir'] . '/mpdf_temp/';

if (!file_exists($custom_temp_dir)) {
    mkdir($custom_temp_dir, 0755, true);
}
$mpdf = new \Mpdf\Mpdf(['tempDir' => $custom_temp_dir]);
$mpdf->WriteHTML("<h1>test</h1>");
$file='abcd.pdf';
$mpdf->output($file,'F');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文