开发者

ImageMagick: Tiff to PDF from PHP

开发者 https://www.devze.com 2022-12-16 08:00 出处:网络
How can I convert 2 tiff images to PDF, I already knows how to get the image out of the DB, and I print it using echo and setting up the MIME type.

How can I convert 2 tiff images to PDF, I already knows how to get the image out of the DB, and I print it using echo and setting up the MIME type.

But, right kn开发者_如何学运维ow I need to use a duplex printer option, so I need a way to generate a PDF from inside my PHP page, that PDF must containt both TIFF images (one per page) How can I do that? What do I need for php to work with that library.

Thank you very much.

EDIT:

Is a self hosted app, I own the server (actually I'm using WAMP 2).

I extract the images from the MySQL DB (stored using LONGBLOBS).


There is a very simple PHP script that interfaces with ImageMagick:

How to convert multipage TIFF to PDF in PHP

I haven't used it myself but it looks all right. For this you will need

  • ImageMagick installed
  • Ghostscript installed

the linked article describes how to install those in a Ubuntu Linux environment.

Another road to take would be inserting the images directly into a auto-generated PDF file without ImageMagick. The best-known PDF generation library, FPDF, can do this, but for JPEG, PNG and GIF only.

Maybe one of these works for you.


What you really need is a library that brings you a PDF composition engine. And of course you need that engine to support image insertions (specifically TIFF).

The best option is iText.

public void createPdf(String filename) throws DocumentException, IOException 
{
   // step 1
    Document document = new Document();
    // step 2
    PdfWriter.getInstance(document, new FileOutputStream(filename));
    // step 3
    document.open();
    // step 4
    document.add(new Paragraph("PDF Title"));
    // step 5
    document.add(new Image("Tiff image path..."));
    // step 6
    document.close();
}

Hope it helps!


Using imagick library, below solution worked for me -

$document = new Imagick($path."/".$fileName.tiff);
$data = $document->getImageBlob();
$document->setImageFormat("pdf");
$document->writeImages($path."/".$fileName.pdf, true);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号