output a PDF file using imagic and PHP

孤街醉人 提交于 2019-12-24 15:03:04

问题


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;

回答1:


$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');



回答2:


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.



来源:https://stackoverflow.com/questions/3870973/output-a-pdf-file-using-imagic-and-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!