简体   繁体   English

使用ImageMagick将图像合并为PDF页面

[英]Combine images to PDF pages with ImageMagick

I've created a multiple page PDF where I was supposed to add dynamic text to one of the pages (2nd of 3 pages). 我创建了一个多页PDF,应该在其中将动态文本添加到其中一个页面(第3页,第2页)中。 When I found out my shared host doesn't support PDF manipulation, I had to resort to using ImageMagick, convert each page to its own PNG file. 当我发现共享主机不支持PDF操作时,我不得不求助于ImageMagick,将每个页面转换为自己的PNG文件。

Adding text using ImageMagick is a dream, but I want to add the front page and back page. 使用ImageMagick添加文本是一个梦想,但是我想添加首页和背面。 I need to combine page1.png , page2.png (with added text) and page3.png to output.pdf but I can't find the right code for doing it with PHP Imagick() class. 我需要将page1.pngpage2.png (带有添加的文本)和page3.pngoutput.pdf但是我找不到使用PHP Imagick()类执行此操作的正确代码。

I thought I was doing the right when I used the addImage() function, but this wouldn't do it. 当我使用addImage()函数时,我以为我做对了,但是这样做不会。

What I tried: 我试过的

<?php

$combined = new Imagick();

$frontpage = new Imagick('png/frontpage.png');
$backpage = new Imagick('png/backpage.png');

$insidepage = new Imagick('png/inside.png');
$draw = new ImagickDraw();

$draw->setFillColor('black');

$draw->setFont('fonts/Georgia-Bold-Italic.ttf');
$draw->setFontSize(44);
$insidepage->annotateImage($draw, 2020, 268, 0, 'Text goes here');

$draw->setFont('fonts/Georgia-Italic.ttf');
$insidepage->annotateImage($draw, 2020, 987, 0, 'Text goes here');

header('Content-Type: application/pdf');

$combined->addImage( $frontpage );
$combined->addImage( $insidepage );
$combined->addImage( $backpage );

$combined->setImageFormat('pdf');

echo $combined;

While browsing for another problem I saw this old one which is incompletely answered. 在浏览另一个问题时,我看到这个旧问题没有得到完全回答。 For the sake of completeness: The issue is not, that the file has to be written to file storage before displaying on the users browser, rather the usage of echo is causing the empty browser preview. 出于完整性考虑:问题不在于文件必须在用户浏览器上显示之前必须写入文件存储中,而是使用echo会导致浏览器预览空白。

Use echo $combined->getImageBlob(); 使用echo $combined->getImageBlob(); instead of echo $combined; 而不是echo $combined; .

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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