开发者

output a PDF file using imagic and PHP

开发者 https://www.devze.com 2023-01-19 02:07 出处:网络
I have some PDF\'s that are stored in a SQL server with data type Image. Now I want to merge them into a single document with Imagic from a 开发者_运维技巧PHP page. Here is the code:

I have some PDF's that are stored in a SQL server with data type Image. Now I want to merge them into a single document with Imagic from a 开发者_运维技巧PHP page. Here is the code:

$combined   =   new Imagick();

while( $document    =   mssql_fetch_assoc( $mssqlResult ){
    $image      =   new Imagick(); 
    $image->readImageBlob( $document['Contents'] ) ;
    $combined->addImage( $image );
    $image->clear();
    $image->destroy(); 
}
$combined->setImageFormat("pdf");
$combined->writeImages( 'test.pdf', true );

This works, and the test.pdf is saved to the server, but when I try to output the browser URL (something like http://www.test.com/test.php), it does not work. The code is:

$combined   =   new Imagick();

while( $document    =   mssql_fetch_assoc( $mssqlResult ){
    $image      =   new Imagick(); 
    $image->readImageBlob( $document['Contents'] ) ;
    $combined->addImage( $image );
    $image->clear();
    $image->destroy(); 
}

//$combined->getImageBlob();

//$combined->setImageFormat("pdf");

//$combined->writeImages( 'test.pdf', true );

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

header('Content-Disposition: attachment; filename="test.pdf"');

echo $combined;


$combined   =   new Imagick();

while( $document    =   mssql_fetch_assoc( $mssqlResult ){
    $image      =   new Imagick(); 
    $image->readImageBlob( $document['Contents'] ) ;
    $combined->addImage( $image );
    $image->clear();
    $image->destroy(); 
}

$combined->getImageBlob();

$combined->setImageFormat("pdf");

$combined->writeImages( 'test.pdf', true );

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

header('Content-Disposition: attachment; filename="test.pdf"');

echo file_get_contents('test.pdf');


This works:

combined   =   new Imagick();

while( $document    =   mssql_fetch_assoc( $mssqlResult ){
    $image      =   new Imagick(); 
    $image->readImageBlob( $document['Contents'] ) ;
    $combined->addImage( $image );
    $image->clear();
    $image->destroy(); 
}

$combined->setImageFormat("pdf");

header('Content-type: application/pdf');
header('Content-Disposition: attachment; filename="test.pdf"');
echo $combined->getImagesBlob();

Note that the key word is getImagesBlob, not getImageBlob.

0

精彩评论

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