java 获取文件返回 pdf文件并打印

<%@ page language="java" import="java.util.*,java.io.*"
pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://"
    + request.getServerName() + ":" + request.getServerPort()
    + path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
   <base href="<%=basePath%>">
</head>
<%
   out.clear();
   out = pageContext.pushBody();
   response.setContentType("application/pdf");
   try {
       String _fileName=request.getAttribute("fileName").toString();
      String strPdfPath = new String(_fileName);
    //判断该路径下的文件是否存在
    File file = new File(strPdfPath);
    if (file.exists()) {
     DataOutputStream temps = new DataOutputStream(response
       .getOutputStream());
     DataInputStream in = new DataInputStream(
       new FileInputStream(strPdfPath));

     byte[] b = new byte[2048];
     while ((in.read(b)) != -1) {
      temps.write(b);
      temps.flush();
     }
     in.close();
     temps.close();
    } else {
     out.print(strPdfPath + " 文件不存在!");
    }
   } catch (Exception e) {
    out.println(e.getMessage());
   }
%>
<body>
   <br>
</body>

</html>

java代码只需要查询当前的文件,获取到文件的路径+名称 放入model,在页面打印出来即可 生成pdf文件打印出来

好的,以下是一个简单的示例代码,用于生成并打印 PDF 文件: ```java import java.io.FileOutputStream; import com.itextpdf.text.Document; import com.itextpdf.text.Paragraph; import com.itextpdf.text.pdf.PdfWriter; import java.awt.print.PrinterJob; import javax.print.PrintService; public class PDFGenerator { public static void main(String[] args) { try { // 创建文档对象 Document document = new Document(); // 设置输出路径 String outputPath = "C:/pdf/demo.pdf"; PdfWriter.getInstance(document, new FileOutputStream(outputPath)); // 打开文档对象 document.open(); // 添加段落 document.add(new Paragraph("Hello World!")); // 关闭文档对象 document.close(); // 输出提示信息 System.out.println("PDF文件已生成并保存到:" + outputPath); // 打印PDF文件 printPDF(outputPath); } catch (Exception e) { e.printStackTrace(); } } // 打印PDF文件 public static void printPDF(String filePath) throws Exception { // 获取打印机服务 PrintService printService = PrinterJob.getPrinterJob().getPrintService(); // 创建打印任务 PrinterJob job = PrinterJob.getPrinterJob(); // 设置打印机 job.setPrintService(printService); // 设置打印文件 job.setPrintable(new PDFPrintable(filePath)); // 执行打印任务 job.print(); } } // PDF打印器 class PDFPrintable implements Printable { private PDDocument document; public PDFPrintable(String filePath) throws IOException { // 加载PDF文件 this.document = PDDocument.load(new File(filePath)); } @Override public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException { // 获取PDF页码 int pageCount = document.getNumberOfPages(); if (pageIndex >= pageCount) { return NO_SUCH_PAGE; } // 获取PDF页面 PDPage page = document.getPage(pageIndex); // 创建Graphics2D对象 Graphics2D graphics2D = (Graphics2D) graphics; // 获取打印区域大小 Rectangle2D.Double rect = new Rectangle2D.Double( pageFormat.getImageableX(), pageFormat.getImageableY(), pageFormat.getImageableWidth(), pageFormat.getImageableHeight()); // 渲染PDF页面 PDFRenderer renderer = new PDFRenderer(page.getDocument()); BufferedImage image = renderer.renderImage(pageIndex, 1.0f); // 绘制PDF页面 graphics2D.drawImage(image, null, (int) rect.x, (int) rect.y); // 返回打印结果 return PAGE_EXISTS; } } ``` 这个示例使用了 iText 库来生成 PDF 文件,使用了 Java 的打印机 API 来打印 PDF 文件。示例中首先创建了一个文档对象,然后使用 PdfWriter 将其输出到指定的本地目录,接着添加了一个段落,最后关闭文档对象。示例中使用了字符串类型的 outputPath 变量来指定生成 PDF 文件的路径,你可以根据自己的需求修改该变量的值,指定生成 PDF 文件的具体路径。 当程序运行完成后,生成的 PDF 文件会自动保存到指定的本地目录中,并输出一条提示信息。接着,程序调用了 printPDF 方法,该方法使用了 Java 的打印机 API 来打印 PDF 文件printPDF 方法中首先获取了系统中的打印机服务,然后创建了一个打印任务对象,设置了打印机和打印文件,最后调用了打印任务的 print 方法来执行打印任务。 注意,如果你要使用这个示例代码来打印 PDF 文件,你需要先在系统中安装一个 PDF 打印机服务,例如 Adobe PDF 或者 Foxit Reader PDF Printer 等,否则程序会抛出异常。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值