使用imagic和PHP输出PDF文件

时间:2010-10-06 09:06:32

标签: php imagick

我有一些PDF存储在数据类型为Image的SQL服务器中。 现在我想从PHP页面将它们合并到一个带有Imagic的文档中。这是代码:

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

这样做,并且test.pdf保存到服务器,但是当我尝试输出浏览器URL(类似http://www.test.com/test.php)时,它不起作用。代码是:

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

2 个答案:

答案 0 :(得分: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');

答案 1 :(得分:0)

这有效:

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();

请注意,关键字是getImagesBlob,而不是getImageBlob