简体   繁体   中英

Create PDF from PNG with ImageMagick

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM