简体   繁体   English

如何自动打印PDF

[英]How to print PDFs automatically

We have a number of systems that produce PDFs that need to be printed. 我们有许多系统可以生成需要打印的PDF。 These are stored on a central document store. 它们存储在中央文档存储中。 A message then goes onto a JMS queue that the document needs printing. 然后,消息将进入文档需要打印的JMS队列。 A service, written in Java , picks these up and then invokes a native command. 用Java编写的服务会选择这些服务,然后调用本机命令。 This is to call Adobe Reader with the /t flag. 这是使用/ t标志调用Adobe Reader。 This causes the document to print without the GUI showing. 这会导致文档打印而不显示GUI。

However since a power cut this no longer works. 但是,由于断电,这不再起作用。 In the interim we are having to manually print hundreds of documents. 在此期间,我们不得不手动打印数百个文档。 We originally tried using Java printing, but the PDFs came out malformed. 我们最初尝试使用Java打印,但PDF格式错误。

What is a better solution to this? 有什么更好的解决方案?

This code only works if the printer supports PDF. 此代码仅在打印机支持PDF时有效。 Otherwise you need to use a native printer or a Java library. 否则,您需要使用本机打印机或Java库。 There is a blog article on this at http://pdf.jpedal.org/java-pdf-blog/bid/25566/Printing-PDF-files-from-Java 有一篇关于此的博客文章,请访问http://pdf.jpedal.org/java-pdf-blog/bid/25566/Printing-PDF-files-from-Java

Show us the code. 告诉我们代码。 I remember printing PDF with no issues using Java Print API. 我记得使用Java Print API打印PDF没有任何问题。 Below is the code, might need some modification, but should run as it is, 下面是代码,可能需要一些修改,但应该按原样运行,

        InputStream in = new FileInputStream(file);
        DocFlavor flavor = DocFlavor.INPUT_STREAM.PDF;

        // find the printing service
        AttributeSet attributeSet = new HashAttributeSet();
        attributeSet.add(new PrinterName("FX", null));
        attributeSet.add(new Copies(1));

        PrintService[] services = PrintServiceLookup.lookupPrintServices(
                DocFlavor.INPUT_STREAM.PDF, attributeSet);

        //create document
        Doc doc = new SimpleDoc(in, flavor, null);

        // create the print job
        PrintService service = services[0];
        DocPrintJob job = service.createPrintJob();

        // monitor print job events
        PrintJobWatcher watcher = new PrintJobWatcher(job);

        System.out.println("Printing...");
        job.print(doc, null);

        // wait for the job to be done
        watcher.waitForDone();
        System.out.println("Job Completed!!");

Note: 注意:

  • Flavor is not needed in 2 places, 1 place should be enough. 2个地方不需要Flavor ,1个地方就足够了。 You find that out. 你找到了。
  • PrintJobWatcher is a nested class, to add a PrintJobListener . PrintJobWatcher是一个嵌套类,用于添加PrintJobListener

Try using ICEpdf . 尝试使用ICEpdf Here's an example from documentation page : 以下是文档页面中的示例:

Document pdf = new Document();
pdf.setFile(filePath);

// create a new print helper with a specified paper size and print
// quality
PrintHelper printHelper = new PrintHelper(null, pdf.getPageTree(),
        0f, MediaSizeName.NA_LEGAL, PrintQuality.DRAFT);
// try and print pages 1 - 10, 1 copy, scale to fit paper.
printHelper.setupPrintService(selectedService, 0, 0, 1, true);
// print the document
printHelper.print();

You can use Apache PDFBox . 您可以使用Apache PDFBox Examples: 例子:

a) Printing PDF as Pageable 一)打印PDF作为Pageable

PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = printService.createPrintJob();

PDDocument pdDocument = PDDocument.load(new File("doc.pdf"));
PDFPageable pdfPageable = new PDFPageable(pdDocument);
SimpleDoc doc = new SimpleDoc(pdfPageable, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);

printJob.print(doc, null);

b) Printing PDF as Printable b)将PDF PrintablePrintable

This option has advantage that you can control page dimensions, margins, etc. by modifying pageFormat variable. 此选项的优点是您可以通过修改pageFormat变量来控制页面尺寸,边距等。

PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
DocPrintJob printJob = printService.createPrintJob();
PageFormat pageFormat = PrinterJob.getPrinterJob().defaultPage();

PDDocument pdDocument = PDDocument.load(new File("doc.pdf"));
PDFPrintable pdfPrintable = new PDFPrintable(pdDocument);

Book book = new Book();
book.append(pdfPrintable, pageFormat);
SimpleDoc doc = new SimpleDoc(book, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);

printJob.print(doc, null);

Ever since Java 1.5, Sun developed a pdf renderer library for handling PDF. 从Java 1.5开始,Sun开发了一个用于处理PDF的pdf渲染器库。 Now this one is left to Swing Labs. 现在这个留给Swing Labs。 And not sure whether this one would be added into future java APIs. 并且不确定这个是否会被添加到未来的Java API中。 http://java.net/projects/pdf-renderer/ http://java.net/projects/pdf-renderer/

It is used to view or print pdf files. 它用于查看或打印pdf文件。 to print pdf files, you can call this libray. 要打印pdf文件,您可以调用此libray。 Here is some part of the code. 这是代码的一部分。

File input = new File(docName);
FileInputStream fis = new FileInputStream(input);
FileChannel fc = fis.getChannel();
ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

PDFFile curFile=null;
PDFPrintPage pages=null;
curFile = new PDFFile(bb); // Create PDF Print Page
pages = new PDFPrintPage(curFile);
PrinterJob pjob = PrinterJob.getPrinterJob();

PrintService[] services = pjob.lookupPrintServices();
for(PrintService ps:services){
    String pName = ps.getName();
    if(pName.equalsIgnoreCase("PrinterName")){
        pjob.setPrintService(ps);
        System.out.println(pName);
        break;
    }
}

pjob.setJobName(docName);
Book book = new Book();
PageFormat pformat = PrinterJob.getPrinterJob().defaultPage();
book.append(pages, pformat, curFile.getNumPages());
pjob.setPageable(book);

// print
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();


// Print it
pjob.print(aset);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM