我有以下代码。它用于将各种图像附件(和pdf)组合成一个PDF。出于某种原因,当我甚至采用单个PDF并将其通过代码时,最终结果与原始PDF相比显得非常糟糕。另外,我可以在源PDF中选择文本,但是在生成的文本中我不能。为什么使用此代码生成的PDF文件的质量如此之低?
任何帮助将不胜感激。
// PDF object
$pdf = new Imagick();
$max_resolution = array('x' => 100, 'y' => 100);
foreach($attachment_ids as $attachment_id) {
$attachment = DAO_Attachment::get($attachment_id);
$file = Storage_Attachments::get($attachment);
// Temporarily store our attachment
$im = new Imagick();
$im->readImageBlob($file);
// We need to reset the iterator otherwise only one page will be rotated
$im->resetIterator();
// Get the resolution
$resolution = $im->getImageResolution();
if($resolution['x'] > $max_resolution['x']) {
$max_resolution['x'] = $resolution['x'];
}
if($resolution['y'] > $max_resolution['y']) {
$max_resolution['y'] = $resolution['y'];
}
$num_pages = $im->getNumberImages();
$rotation = array_shift($rotations);
$degrees = $rotation > 0 ? 360 - $rotation : 0;
$pages = array();
if($degrees > 0) {
// Rotate each page
for($i = 1; $i <= $num_pages; $i++) {
$im->nextImage();
$im->rotateImage(new ImagickPixel(), $degrees);
}
}
// We need to reset the iterator again so all of our pages will be added to the pdf
$im->resetIterator();
// If the image format isn't a pdf, convert it to a png
if($im->getImageFormat !== 'pdf') {
$im->setImageFormat('png');
// Opacity
if(method_exists($im, 'setImageOpacity'))
$im->setImageOpacity(1.0);
}
$im->setImageCompression(imagick::COMPRESSION_LOSSLESSJPEG);
$im->setImageCompressionQuality(100);
$im->stripImage();
// Add the rotated attachment to the PDF
$pdf->addImage($im);
// Free
$im->destroy();
}
// Create a composite
$pdf->setImageFormat('pdf');
// Compress output
$pdf->setImageCompression(imagick::COMPRESSION_LOSSLESSJPEG);
$pdf->setImageCompressionQuality(100);
$pdf->stripImage();
// Set resolution
$pdf->setImageResolution($max_resolution['x'], $max_resolution['y']);
定义“看起来非常糟糕” - 理想情况下,显示一些截图 – 2011-06-02 19:38:04
听起来就像是栅格化PDF,因此它不再是文本嵌入在一个页面中,而是一个图片的一些文本。如果您以显示分辨率(72-100dpi)渲染PDF,当以便宜的激光打印机分辨率(300dpi)打印时,它会显得很糟糕。 – 2011-06-02 19:50:17
不幸的是,我无法显示任何截图。只要说质量很差就足够了。我们不打算这样做,但不管它听起来像是可能会被栅格化......任何想法如何阻止这种行为? – sluther 2011-06-02 20:03:55