问题
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