简体   繁体   English

imagick,php中的pdf分页符imagick

[英]imagick , page break in pdf in php imagick

i am using php imagick to create a image and than convert to pdf. 我正在使用php imagick创建图像,然后将其转换为pdf。 using php imagick. 使用PHP imagick。 i coded this: 我编码为:

    $image = new Imagick();
    $height = 800; //height of the page;
    $image->newImage(794, $height, "#f5f5f5");
    $image->setImageFormat("jpg"); 
    $card = new Imagick('card.jpg'); ; //get single card 

    $l_align = 190; //left alignment
    for($i=0; $i < 4; $i++)  //for creating multiple cards on a page
    {
        $image->compositeImage($card, Imagick::COMPOSITE_DEFAULT,10, ($l_align*$i)+10);
        $image->compositeImage($card, Imagick::COMPOSITE_DEFAULT, 390, ($l_align*$i)+10);
    }
    $image->setResolution(72, 72);
    $image->resetIterator();
    $combined = $image->appendImages(true);


    $image->setImageFormat("pdf");
    $combined->writeImages( 'card.pdf', true );
    header("Content-Type: application/pdf");
    header('Content-Disposition: attachment; filename="card.pdf"');

    echo file_get_contents('card.pdf');

and get something like this 并得到这样的东西 在此处输入图片说明 in pdf format . pdf格式。 Now i want to page break after every 6 cards print in pdf . 现在,我想在以pdf打印每6张卡片后进行分页。 i am using imagick . 我正在使用imagick。 please help me. 请帮我。 thanks in advance. 提前致谢。

You are using the wrong function for adding images as new pages. 您使用错误的功能将图像添加为新页面。 You should be using addImage which adds the new image as a separate page, rather than append which just tacks them onto the bottom of the current image. 您应该使用addImage将新图像添加为单独的页面,而不是附加将其添加到当前图像底部的追加。

An example of this working is: 此工作的一个示例是:

$combined = null;

$images = [
    '../../../images/Source1.jpg',
    '../../../images/Source2.png',
];

foreach ($images as $image) {
    if ($combined == null) {
        $combined = new Imagick(realpath($image));   
    }
    else {
        $card = new Imagick(realpath($image)); ; //get single card 
        $combined->addImage($card);
    }
}

$combined->setImageFormat("pdf");
$combined->writeImages( './card.pdf', true );

btw there is weird stuff going on in your code example - you're only even attempting to add one image, and what is 'resetIterator' doing in there? 顺便说一句,您的代码示例中发生了一些奇怪的事情-您甚至只是尝试添加一个图像,并且那里的“ resetIterator”在做什么?

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

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