I have a working class that creates and saves a PDF file for the user by implementing Printable and using PrinterJob to create the PDF to a set location. This works on a Windows system perfectly but I'm having trouble doing this on a Linux system.
I'm trying to automate the creation of these PDF's on a Linux system as follows:
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable( this );
job.setPrintService( defaultPrinter );
// create a new HashPrintRequestAttributeSet
HashPrintRequestAttributeSet attributes = new HashPrintRequestAttributeSet();
// set the output file as a destination
attributes.add(new Destination(new File(fileName).toURI()));
job.print(attributes);
I have installed cups-pdf on my Linux system and pointed the defaultPrinter
to that PrintService
. The file gets created but it is not a PDF file but a Postscript file. CUPS doesn't even create a print job for this so I believe it is just creating a postscript file with printer commands.
The Postscript file starts as follows:
%!PS-Adobe-3.0
%%BeginProlog
/imStr 0 def /imageSrc {currentfile /ASCII85Decode filter /RunLengthDecode filter imStr readstring pop } def
/BD {bind def} bind def
/D {def} BD
/C {curveto} BD
/L {lineto} BD
/M {moveto} BD
/R {grestore} BD
/G {gsave} BD
/N {newpath} BD
/P {closepath} BD
/EC {eoclip} BD
/WC {clip} BD
/EF {eofill} BD
/WF {fill} BD
/SG {setgray} BD
/SC {setrgbcolor} BD
...
I know I can use PDFBox to draw the PDF myself but all the code is there for creating a PDF file and it all works on windows. Is it possible to get a linux system to create a PDF and save it to a location using PrinterJob?
Turns out that it's only windows systems that need attributes. If you use attributes on Linux system, it will only save the printer commands rather than the PDF.
If you have lpr
on your linux system and call job.print();
, it will create the PDF that way, give the job a name so you can locate it on your system.
Had to install cups-pdf and cups-bsd.