ImageMagick:从PHP转换为PDF

问题描述 投票:1回答:3

如何将2个tiff图像转换为PDF,我已经知道如何将图像从数据库中取出,并使用echo和设置MIME类型来打印它。

但是,我知道我需要使用双面打印机选项,所以我需要一种从PHP页面内部生成PDF的方法,该PDF必须包含两个TIFF图像(每页一个)。我该怎么做?我需要PHP与该库一起使用。

非常感谢。

编辑:

是一个自托管的应用程序,我拥有服务器(实际上我正在使用WAMP 2)。

我从MySQL数据库(使用LONGBLOBS存储)中提取图像。

php pdf imagemagick tiff
3个回答
1
投票

您真正需要的是一个为您带来PDF合成引擎的库。当然,您需要该引擎来支持图像插入(特别是TIFF)。

最佳选择是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();
}

希望有帮助!


2
投票

有一个非常简单的与ImageMagick交互的PHP脚本:

How to convert multipage TIFF to PDF in PHP

我没有亲自使用过,但看起来还不错。为此,您需要

  • 已安装ImageMagick
  • 已安装Ghostscript

链接的文章描述了如何在Ubuntu Linux环境中安装它们。

另一种方法是将图像直接插入没有ImageMagick的自动生成的PDF文件中。最著名的PDF生成库FPDF可以做到这一点,but for JPEG, PNG and GIF only

也许是您的其中一项作品。


0
投票

使用imagick库,以下解决方案对我有用-

$document = new Imagick($path."/".$fileName.tiff);
$data = $document->getImageBlob();
$document->setImageFormat("pdf");
$document->writeImages($path."/".$fileName.pdf, true);
© www.soinside.com 2019 - 2024. All rights reserved.