Convert from PDF to Postscript using Java

I have a J2EE based application where I use the PDF report tool. I need a Java utility / tool that can help me convert a PDF file to postscript format so that I can print it to a printer ... My application will run on different OSs (i.e. Windows / Linux / AIX), therefore it is important platform independent solution. My reporting tool does not support PS output. Please advice ...

+5
source share
7 answers

For this, AFAIK exist for pure Java solutions without fools, but if the conversion should be done on the server side, I would recommend that you use Ghostscript's pdf2ps converter. Even if you need to install Ghostscript version for a specific platform, you should find it for all mentioned platforms.

+2
source

There are several PDF libraries that can print PDF files. If you print on a Postscript printer and use the print to file option, you can get Postscript.

0
source

"exe based", Ghostscript, - Xpdf.PdfToPS
PdfToPs - . Win, Linux Solaris.
http://www.foolabs.com/xpdf/

0

fop xslfo http://xmlgraphics.apache.org/fop/fop-pdf-images.html

fop test.fo -ps out.ps

test.fo:

<?xml version="1.0" encoding="utf-8"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">

  <fo:layout-master-set>
    <fo:simple-page-master master-name="simple">
      <fo:region-body />
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="simple">
    <fo:flow flow-name="xsl-region-body">

   <fo:block>
    <fo:external-graphic src="my.pdf"/>
      </fo:block> 

    </fo:flow>
  </fo:page-sequence>
</fo:root>
0

Java. CentOS, SUSE Windows 7. - .

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();
            }
        }
}

:

dependencies {
    compile group: 'org.apache.pdfbox', name: 'pdfbox', version: '2.0.8'
}
0

, jasper.

(PDF, Html, RTF).

.

Edit:

, , JRPrintServiceExporter, PDF. , .

-1

, :

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);

        if(factories.length == 0)
        {
            System.err.println ("No PS factories available!");
            System.exit(0);
        }

        // Open the PDF file
        PDFPrint pdfPrint = new PDFPrint ("test.pdf", null);

        // Open the output file
        FileOutputStream fos = new FileOutputStream("output.ps");

        // Use the first service available
        StreamPrintService sps = factories[0].getPrintService(fos);
        DocPrintJob pj = sps.createPrintJob();

        // Define paper size
        PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
        aset.add(MediaSizeName.NA_LETTER);

        // Create simple doc using PDFPrint as Printable and print it
        SimpleDoc doc = new SimpleDoc(pdfPrint, flavor, null);
        pj.print(doc, aset);

        // Close the output PS stream
        fos.close();
    }
    catch (Throwable t)
    {
        t.printStackTrace();
    }
-1

All Articles