in reply to How to edit a PCL file

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.

Replies are listed 'Best First'.
Re^2: How to edit a PCL file
by GoForIt (Novice) on Apr 01, 2010 at 13:50 UTC

    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