dongxun1142
2014-01-19 13:51 阅读 52
已采纳

使用ImageMagick从PNG创建PDF

I have installed ImageMagick on my server for the purpose of converting a PNG image to PDF. I came across this code:

convert image1.png image2.png image3.png output.pdf

Ref. http://www.wizards-toolkit.org/discourse-server/viewtopic.php?f=1&t=16778

I am not sure how to run this from PHP. Also, I don't have image files, but rather, I have a stream like so:

<?php
$im = imagecreatefrompng("test.png");

header('Content-Type: image/png');

imagepng($im);
imagedestroy($im);
?>

Can someone tell me how to convert this image stream, $im to PDF using ImageMagick? I've tried google'ing but I haven't found any examples. Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    douda5227 douda5227 2014-01-19 14:42

    This worked for me:

    $yourPngImage = imagecreatefrompng("test.png");
    $im = new Imagick();
    
    ob_start();
    imagepng($yourPngImage);
    $image_data = ob_get_contents();
    ob_end_clean();
    
    // Get image source data
    $im->readimageblob($image_data);
    
    $im->setImageFormat('pdf');
    
    header("Content-Type: application/pdf");
    echo $im;
    
    点赞 评论 复制链接分享

相关推荐