You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Imagick将多图转为PDF:如何实现每张图片单独占一页

解决PDF单页多图问题,实现每张图片单独占一页

问题说明

通过Ajax传递的$_POST['images']包含多张图片的DataURL,当前生成的PDF会将所有图片放在同一页面,需求调整为每张图片单独占据一页。

修改后的代码

$images = $_POST['images'];

$pdf = new Imagick();
$pdf->setFormat('pdf');

foreach ($images as $dataURL) {
    $data = substr($dataURL, strpos($dataURL, ',') + 1);
    $image = base64_decode($data);

    $imagick = new Imagick();
    $imagick->readImageBlob($image);

    $pdf->addImage($imagick);
}

// 移除合并图片的操作,保留独立页面
header('Content-Type: application/pdf');
echo $pdf->getImagesBlob();
$pdf->destroy();

关键修改点

  • 移除$pdf->appendImages(true):这行代码会把所有添加的图像合并成单个页面,正是导致多图挤在一页的核心原因。直接通过addImage添加的每张图片,会自动成为PDF的独立页面。
  • 修正拼写错误:将HTTP头里的applicaton改为正确的application,将销毁方法destory改为destroy。

内容的提问来源于stack exchange,提问作者Mathemagician

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 22:47:17