GoForIt has asked for the wisdom of the Perl Monks concerning the following question:


Hi there,

Could someone tell me if it's safe to edit PCL(Printer Command Language) files in Windows? I've a bunch of PCL files to edit and each PCL file is different when it comes to the header part. So, I'm not able to figure out a standard simple way to edit these files. I use regular expressions to find a string and replace it with the value I want. When I try to replace more strings, things get messed up and the document cannot be read. Something goes wrong with the special characters though I'm not trying to edit them.

Could someone throw few pointers on this one? Are there better ways of doing this? This would help me a lot.


Thanks,
Thilak

Replies are listed 'Best First'.
Re: How to edit a PCL file
by Corion (Patriarch) on Apr 01, 2010 at 13:07 UTC

    If you use binmode on your input and output filehandles, you should be able to do a "non-edit" of your file, loading and saving it unmodified, and it should still work.

    I'm not sure, but I think I remember that PCL is not all that trivial to generate and edit, so depending on your task, simple string replacements may or may not work. But that depends on the kind of replacements you do.

      Yes, binmode does help me in reading the characters as they are. But I'm not sure as to how many bytes I should read at a time. Since PCL files are not regular ASCII files and they are not delimited on "\n", reading them one line at a time is not a simple thing unless I know the line delimiter.

      I've been trying to find the line and page delimiter from those characters in the file and using PCL command reference, but I couldn't succeed.

      I figured all the commands begin with ESC control code.

        GoForIt:

        There aren't line delimiters in PCL, since the printer doesn't care about "lines" in a control file. Just read the file in fixed-length chunks to do your manipulations. It shouldn't matter how many bytes you process at a time, so I'd choose a relatively large buffer (32K or so).

        ...roboticus

        I figured all the commands begin with ESC control code.

        Since each command starts with the escape character, set $/ = chr( 27 ).

        I dumped a 300 page pdf to a PCL5 printer and the ran over the 300MB file produced counting the length of each 'command' using the following one-liner:

        perl -e"BEGIN{ binmode STDIN, ':raw' }" -l -033 -ne"$l{length()}++}{ print $_, '::', $l{$_} for sort{$a<=>$b} keys %l" + HP-PCL5.bin

        The results showed that the maximium individual readline length was <2MB, with the vast majority being less than 1k:

        0::1048 1::610 2::332 3::2763 4::15235 5::902 6::4896 7::2195 8::2448 9::955 10::9585 11::735 12::766 13::1399 14::30537 15::2323 16::1089 17::399 18::54322 19::1879 20::611 21::171 22::107 23::408 24::1071 25::720 26::738 27::1748 28::1374 29::2025 30::5380 31::3359 32::4704 33::2684 34::4027 35::2016 36::2349 37::1011 38::672 39::1 40::338 41::338 42::2 43::336 44::1 45::335 47::2 49::2 50::1 51::1 54::335 55::1 56::1 57::3 58::335 59::338 60::334 61::336 62::1002 63::1 64::335 67::334 68::335 69::337 72::334 80::1 83::1 90::1 94::1 122::10 230::2 232::6 233::2 242::74 243::2 352::2 353::2 356::2 357::2 362::9 363::2 788::1 2627::1 3363::1 4019::2 6929::1 7045::1 7163::1 7445::1 7972::1 9683::1 9703::1 9726::1 9754::1 ... 998164::1 1001945::1 1008451::1 1016923::1 1017472::1 1019396::1 1023221::1 1030445::1 1032125::1 1044854::1 1049920::1 1056328::1 1056862::1 1060228::1 1063015::1 1065778::1 1066362::1 1067272::1 1068292::1 1069415::1 1070494::1 1071336::1 1072448::1 1072832::1 1073203::1 1075300::1 1076627::1 1082431::1 1083135::1 1097793::1 1098097::1 1100634::1 1101911::1 1103123::1 1107700::1 1109616::1 1111129::1 1113723::1 1116752::1 1126199::1 1129660::1 1132176::1 1136614::1 1142884::1 1144724::1 1162069::1 1164751::1 1167526::1 1172941::1 1174051::1 1174803::1 1178963::1 1181757::1 1209556::1 1227153::1 1241712::1 1241953::1 1255729::1 1264951::1 1265598::1 1267717::1 1269103::1 1269839::1 1273652::1 1275352::1 1278748::1 1279336::1 1291701::1 1298282::1 1305369::1 1326645::1 1355690::1 1361723::1 1373787::1 1382753::1 1393848::1 1433523::1 1465612::1 1571234::1 1586972::1 1595487::1 1620081::1 1630469::1 1643933::1 1653299::1 1694273::1 1712694::1
Re: How to edit a PCL file
by marto (Cardinal) on Apr 01, 2010 at 13:22 UTC

    What is generating this PCL? A cleaner approach may be to edit the source file either by the use of a module or automating the source application.

      Hi,

      Thanks for responding. I've searched for CPAN modules but there isn't any that will edit the PCL files. There's one PCL::Simple to create a PCL file. So, I'm out of option there.

      The header of one such file says this: ESC%-12345X@PJL COMMENT HP LaserJet 4100 Series PCL

      I read somewhere that most of the PCL files begin with this statement but again some files miss this information. I've referred to PCL command reference document to understand the characters in the file. But it appears too tedious to follow the entire PCL file.

      Could you tell me what you mean by automating the source application?

      Thanks,
      Thilak
        I've referred to PCL command reference document to understand the characters in the file. But it appears too tedious to follow the entire PCL file.

        I don't think there really are any other options.  As PCL consists of escape sequences (of varying length) intermixed with graphics (pixel) data and maybe plain text, you won't have much luck finding a "record" separator.  In other words, to really know what is what in the file, you'd have to parse or "understand" each escape sequence...

        What exactly do you need to edit?  (and why, me wonders...)

        I was trying to understand where these PCL files are coming from, and what you want to change.

        Say you were using an application, for example Microsoft Word to create documents and save them as PCL files (you can setup a 'dummy' printer driver to output a pcl file to a certain directory. In such a scenario I'd prefer to make the changes you need in the source word document by automating it via Win32::OLE. This solution is easier to maintain, PCL has various version (1-6), PCL files aren't as easy to edit as a text/Word/PDF file.