• java从远程服务器获取PDF文件并后台打印(使用pdfFox)


    一、java原生方式打印PDF文件

    正反面都打印,还未研究出只打印单面的方法,待解决

    public static void printFile(String path) throws Exception {
            File file = new File(path);
            File[] fies=file.listFiles();
                for(File f:fies){
                System.out.println("file "+f.getName());
                     String fileExt=f.getName().substring(f.getName().indexOf(".")+1,f.getName().length());
                      if("pdf".equalsIgnoreCase(fileExt)){
                        String filepath=path+File.separator+f.getName();
                        File pdfFile=new File(filepath);
                        //构建打印请求属性集
                        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
                        //设置打印格式,因为未确定文件类型,这里选择AUTOSENSE
                        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
                        //查找所有的可用打印服务
                        PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
                        //定位默认的打印服务
                        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
                         //显示打印对话框  
                        //PrintService service = ServiceUI.printDialog(null, 200, 200, printService,   defaultService, flavor, pras);
                        if(defaultService!=null){
                            DocPrintJob job = defaultService.createPrintJob(); //创建打印作业
                            FileInputStream fis = new FileInputStream(pdfFile); //构造待打印的文件流
                            DocAttributeSet das = new HashDocAttributeSet();
                            Doc doc = new SimpleDoc(fis, flavor, das); //建立打印文件格式
                            job.print(doc, pras); //进行文件的打印
                    }
                        f.delete();
                }
            }
          }
    
        public static void main(String[] args) {
            //System.out.println("Value:"+test());
            //打印pdf的一个方法,首先安装下PDFCreator软件
            
            try {
                printFile("D:"+File.separator);
            } catch (Exception e) {
                System.out.println("打印文件异常:"+e.getMessage());
                e.printStackTrace();
            }
            
        }

    二、使用pdfBox方式此问题解决

    1.maven依赖

    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.3</version>
    </dependency>

    2. coding

    import java.awt.print.Book;
    import java.awt.print.PageFormat;
    import java.awt.print.Paper;
    import java.awt.print.PrinterException;
    import java.awt.print.PrinterJob;
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import javax.print.attribute.HashPrintRequestAttributeSet;
    import javax.print.attribute.PrintRequestAttributeSet;
    import javax.print.attribute.standard.PageRanges;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.printing.PDFPageable;
    import org.apache.pdfbox.printing.PDFPrintable;
    
    public final class PrintUtils
    {
        public static void main(String[] args) {
            String fileURL = "http";
            String fileName = ".pdf文件";
            downloadFile(fileURL,fileName);
            printWithDialog(getFilePath(fileName));
        }
    
        public static void printWithDialog(String filePath) {
            try {
                PDDocument document = PDDocument.load(new File(filePath));
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPageable(new PDFPageable(document));
                if (job.printDialog()){
                    job.print();
                }
            } catch (IOException e) {
                e.printStackTrace();
            } catch (PrinterException e) {
                e.printStackTrace();
            }
        }
    
        public static String getFilePath(String fileName){
            File path = new File(System.getProperty("user.dir").concat("/downFile"));
            if (!path.exists() && !path.isDirectory()) {
                path.mkdir();
            }
            String filePath = path + "/" + fileName;
            return filePath;
        }
    
        public static boolean downloadFile(String fileURL,String fileName) {
            try {
                URL url = new URL(fileURL);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                DataInputStream in = new DataInputStream(connection.getInputStream());
                DataOutputStream out = new DataOutputStream(new FileOutputStream(getFilePath(fileName)));
                byte[] buffer = new byte[4096];
                int count = 0;
                while ((count = in.read(buffer)) > 0) {
                    out.write(buffer, 0, count);
                }
                out.close();
                in.close();
                return true;
            } catch (Exception e) {
                return false;
            }
        }
    
    }
  • 相关阅读:
    vs2005入门 之 数据类型(日期类型)[视频]
    vs2005入门准备工作
    vs2005入门 之 定义类、创建及使用对象[视频]
    Atlas入门使用服务端定时控件TimerControl [视频]
    js入门·动态的时钟,显示完整的一些方法,新年倒计时
    Visual Studio 2005入门 之 函数兼本章总结 [视频]
    vs2005入门 之 数据类型/数值/字符/字符串[视频]
    javascript入门·Document对象入门讲解(访问表单,创建新页,获取页标题)
    Atlas入门将普通控件升级为Atlas控件/使用listView显示数据库数据[视频]
    javascript入门·图片对象(无刷新变换图片)\滚动图像
  • 原文地址:https://www.cnblogs.com/daijiabao/p/12055680.html
Copyright © 2020-2023  润新知