0

I am looking into creating PostScript file from a pdf as byte[], to send to printer.

I've checked this post and the reply from Dustin seem to be somewhat similar to what I need. Convert from PDF to Postscript using Java

What I got however is a byte[] that will output a pdf from FileOutputStream. But I want to convert to PostScript. I can't get this code referenced to work, can anybody advise?

import java.io.File;
import java.io.FileOutputStream;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.SimpleDoc;
import javax.print.StreamPrintService;
import javax.print.StreamPrintServiceFactory;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;

public class Printing {


        public static void main(String[] args) {
            try {
            DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
            String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();
            StreamPrintServiceFactory[] factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(flavor, psMimeType);

            System.out.println ("Available PS services: " + factories.length);
            System.out.println ("Format: " + factories[0].getOutputFormat());

            FileOutputStream outStream = new FileOutputStream("/path/to/your.ps");
            StreamPrintService printService = factories[0].getPrintService(outStream);


            PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
            aset.add(MediaSizeName.NA_LETTER);              

            PDDocument doc = PDDocument.load(new File("/path/to/my.pdf"));

            SimpleDoc pdfDoc = new SimpleDoc(new PDFPrintable(doc, Scaling.SCALE_TO_FIT, false), flavor, null);

            DocPrintJob newJob = printService.createPrintJob();
            newJob.print(pdfDoc, aset);

            outStream.close();

            }
            catch(Exception ex) {
                ex.printStackTrace();
            }
        }
}
CompileNow
  • 79
  • 14
  • But this code above produces a PDF and ps, but how do I append or add the FileOutputStream to these files? Or how do I convert to PS file from PDF? – CompileNow May 15 '22 at 12:29
  • The LEADTOOLS PDF Pro library supports the conversion of PDF to [Encapsulated PostScript (EPS)](https://www.leadtools.com/help/sdk/v22/main/api/encapsulated-postscript-eps.html) image output using Java. Both input and output can be either files or streams. However, the resulting EPS is 8-bit grayscale with no color data so if both your PDF and the printer are colored and you wish to keep the color information, this might not be suitable for you. (Disclaimer: I am an employee of the vendor). If you’d like to try this, a free evaluation is available [here](https://www.leadtools.com/downloads). – Hussam Barouqa May 25 '22 at 09:56

0 Answers0