0

Here is the code:

$im=new Imagick();
$im->readImageBlob($pdf_data);
$im->setImageFormat('pcl');
$b64pages = array();
for ($x = 1;$x <= $im->getNumberImages(); $x++) {
    $im->previousImage();
    $pclpage = $im->getImageBlob();
    $b64page = base64_encode($pclpage);
    $b64pages[] = $b64page;
    $count--;
}

When I send the resulting PCL images directly to a PCL printer on the client side, the output is fuzzy. The PDF is not fuzzy when printed via a windows driver. What is the driver doing that I am not doing to insure that the PCL output is clear?

jtmanteo
  • 151
  • 7
  • best guess - PDF is vector format on text, PCL is a 2 colour (black/white) bitmap - you need to really, really up the DPI to keep the text clear, like 1200+ DPI IIRC ... this means your PCL needs to be much bigger than the original PDF. – CD001 May 27 '14 at 14:39
  • Printint using Windows prints as vector, that code is creating raster (bitmap) images from the pages `$im->getImageBlob()`. You're losing quality. The question should be "how do i set the image to high quality when converting to PCL using PHP Imagick (ImageMagick)?" –  May 27 '14 at 14:39
  • @CD001, I am fine with the PCL being bigger, it's just that when I increase the resolution of using `$im->setResolution()`, it also makes the image dimensions much smaller, and things like `$im->setPage()` and `$im->setSize()` don't help – jtmanteo May 27 '14 at 15:04
  • My ImageMagick is a bit rusty since I went back to the GD library a while ago (less processor hungry) but I think you'd need to `setResolution()` and `resizeImage()` - assuming that you're using a DPI of 1200 **and** the PDF is A4 size, you'd need to do something like `resizeImage(9920, 14030, imagick::FILTER_LANCZOS, 1, true)` – CD001 May 27 '14 at 15:27
  • @CD001 I really appreciate the ideas. I gave this a shot (at 300dpi because 1200 was taking much too long), but it's like the PCL printer expects 72 dpi no matter what I do. If I send it an image larger than 612x902 it will not fit on the standard (8.5inx11in) paper. – jtmanteo May 27 '14 at 16:17
  • @CD001 I ended up using ghostscript + exec to pull this off. It takes a good while and the file goes from 235K to 8meg (!), but it will have to do for now. – jtmanteo May 27 '14 at 20:36
  • ImageMagick would be delegating the processing to Ghostscript anyway - so doing it directly is probably the optimal solution here. Calling PHP to call ImageMagick to call Ghostscript would only add to the processing time. – Danack May 28 '14 at 19:54
  • btw you ought to write up what you did as an answer and then accept it, to help future people. – Danack May 28 '14 at 19:57
  • @Danack I'm not sure I have the ultimate solution yet. [pdf2pcl](https://github.com/Arelius/pdf2pcl) looks like a really interesting solution for generating vector format pcl output. I can build it, but it segfaults on any pdf I try. – jtmanteo May 29 '14 at 11:02
  • @Danack OK, wrote it up. Hopefully its helpful, even if not optimal. – jtmanteo May 30 '14 at 18:29

1 Answers1

1

OK so i found a shell script that invokes ghostscript with all the right parameters here: https://gist.github.com/ongardie/6800587. I installed that on my server. Now my file format conversion php code looks like this:

chdir("../pdf_to_pcl");
$ident = uniqid();
$pdf_filename = $ident.".pdf";
$pdf_file = fopen($pdf_filename,'w+');
fwrite($pdf_file,$pdf_data);
fclose($pdf_file);

exec("./pdf2pcl $pdf_filename");

$pcl_filename = $ident.".pcl";
$pcl_file = fopen($pcl_filename,"r+");
while($dt = fread($pcl_file,4096)) {
    $pcl_data .= $dt;    
}

fclose($pcl_file);
unlink($pdf_filename);
unlink($pcl_filename);

The image data I am converting is in $pdf_data. The pcl formatted data is in $pcl_data. I can open a socket to a RAW print server (port 9100) for a PCL printer and write $pcl_data straight to it and it works. Hope this helps.

jtmanteo
  • 151
  • 7