我想在javascript中打印到pdf文件,其中outputstream和pdfwriter

时间:2018-04-18 09:20:46

标签: javascript ajax

我正在尝试使用ajax(等等)从服务器端控制器(报告)(ireport)创建PDF,并尝试将数据返回到pdfwriter,servletoutputstream和httpservletresponse。 (我不确切知道我在做什么,但我这样做了。)

最初的目的是将服务器端的pdf文件发送到客户端,找到打印机并在没有预览窗口的情况下进行打印。

其中,我写过' application / pdf'在服务器端和' datetype:text'在客户端ajax(如果我不使用ajax数据类型有错误:text) 如果将结果打印到控制台,它们将仅列为未知代码。

目前我正在尝试将其放入iframe框架中。

问题! 1.我该怎么做才能使用发送到服务器的文本字符串 - >客户端是pdf还是脚本代码? (两周前我已经问你了) 2.如何将pdf发送到服务器 - >客户?我想通过直接在代码中表达它而不是下载它来将它应用到屏幕上。要打印出来。

ENG)> //我只使用ajax,因为我不知道任何其他方式



$.ajax({
 url : "url",
 data : JSON.stringify(data),
 dataType : "text",
 type: "POST",
 contentType: 'application/json; charset=utf-8',
async : false,
success: function(result){
// I want to view PDF contents and directly print to PDF.
}
})




public Params createIbExItemLabelReport(HttpServletRequest resq, HttpSession session, Params inParams, HttpServletResponse resp) throws Exception{
   Params outParams = ParamsFactory.createParams(inParams);

   resp.setHeader("Cache-Control", "no-cache");
   resp.setHeader("Pragma", "no-cache");
   resp.setHeader("Expires", "0");

   List<DataRow> list = new ArrayList<DataRow>();
   String reportCd = "15";
   String fileName = "ibExItemLabel"+reportCd+"Report";
   String nullJpgFile = "";

   int flag = 0;
   int nullCheck = 0;
   for(DataRow dr : inParams.getDataTable("dt_data")){

       String imgName = "c:\\WMS\\LABEL\\FIAC021_" +reportCd + ".jpg";

       File f = new File(imgName);
       if (!f.isFile()) {
           flag = 1;
           if(nullCheck != 0){
               nullJpgFile += ", ";
           }
           nullJpgFile += "FIAC021";
           nullCheck++;
           continue;
       }
       String bacodeCd = "FIAC02120180416001";
       dr.setParam("imgName", imgName);
       dr.setParam("bacodeCd", bacodeCd);

       list.add(dr);
   }

   if(flag == 1){
       outParams.setParam("ERROR_FILE", "제품코드 ["+nullJpgFile+"]의 라벨 사이즈" + reportCd + "인 파일이 존재하지않습니다.");
     return outParams;
   }

   String appPath = session.getServletContext().getRealPath("/");
   String pdfPath = null;

   List<DataRow> list2 = new ArrayList<DataRow>();
   for(int i = 0; i < list.size(); i++){

         for(int j = 0; j < list.get(i).getInt("printQty"); j++){
           list2.add(list.get(i));
       }

   }

   Report report = new Report();
   pdfPath = report.reportToPdf(session, list2, fileName);

   outParams.setParam("fileName", pdfPath);

   System.out.println("Found! FileName is ' : "+ pdfPath);
   pdfPath = appPath + pdfPath;
   pdfPath = pdfPath.replace("//", "/");

   ServletOutputStream servletOutput = resp.getOutputStream();

   PdfWriter pdfWriter = null;
   StringBuffer pdfJs = null;
   ByteArrayOutputStream pdfOutput = null;
   InputStream pdfInput = null;
   PdfReader pdfReader = null;
   PdfStamper pdfStamper = null;

   pdfOutput = convertPDFToByteArrayOutputStream(pdfPath);
   int printCopy = 1;
   if (printCopy == 0) {
     printCopy = 1;
   }
   if (printCopy > 1) {
     PdfCopyFields pdfPrintCopy = new PdfCopyFields(pdfOutput);
     for (int i = 0; i < printCopy; i++) {
       pdfPrintCopy.addDocument(new PdfReader(outputToInputStream(pdfOutput)));
     }
     pdfPrintCopy.close();
   }

   pdfInput = outputToInputStream(pdfOutput);
   pdfReader = new PdfReader(pdfInput);

   pdfStamper = new PdfStamper(pdfReader, servletOutput);
   pdfWriter = pdfStamper.getWriter();

   String printerNm = "SINDOH D410 Series PCL";

   pdfWriter.setViewerPreferences(PdfWriter.HideMenubar | PdfWriter.HideToolbar | PdfWriter.HideWindowUI);

   pdfJs = new StringBuffer();
   pdfJs.append("var param=this.getPrintParams();\r");
   pdfJs.append("param.printerName=\"").append(printerNm).append("\";\r");

   pdfJs.append("param.interactive=param.constants.interactionLevel.silent;\r");
   pdfJs.append("param.pageHandling=param.constants.handling.shrink;\r");
   pdfJs.append("this.print(param);\r");
   pdfJs.append("this.closeDoc();");
   pdfWriter.addJavaScript(pdfJs.toString(), false);
   servletOutput.flush();

   Log.debug("servletOutput " );
   if (pdfInput != null) {
       try {
         pdfInput.close();
       } catch (Exception e) {
       }
       pdfInput = null;
     }
     if (pdfOutput != null) {
       try {
         pdfOutput.close();
       } catch (Exception e) {
       }
       pdfOutput = null;
     }
     if (pdfReader != null) {
       pdfReader.close();
       pdfReader = null;
     }
     pdfWriter = null;
     try {
       if (pdfStamper != null) {
         pdfStamper.close();
         pdfStamper = null;
       }
     } catch (Exception e) {
     }
     resp.setHeader("Content-Disposition", "inline; filename="+pdfPath);
     resp.setHeader("Content-Type", "application/pdf; charset=UTF-8");
     resp.setCharacterEncoding("UTF-8");

     Log.debug("before outParams " );
   return outParams;
    }


   private InputStream outputToInputStream(ByteArrayOutputStream source) {
   return new ByteArrayInputStream(source.toByteArray());
}



private static ByteArrayOutputStream convertPDFToByteArrayOutputStream(String FilePath) {

   InputStream inputStream = null;
   ByteArrayOutputStream baos = new ByteArrayOutputStream();
   try {

       inputStream = new FileInputStream(new File(FilePath));

       byte[] buffer = new byte[1024];
       baos = new ByteArrayOutputStream();

       int bytesRead;
       while ((bytesRead = inputStream.read(buffer)) != -1) {
           baos.write(buffer, 0, bytesRead);
       }

   } catch (FileNotFoundException e) {
       e.printStackTrace();
   } catch (IOException e) {
       e.printStackTrace();
   } finally {
       if (inputStream != null) {
           try {
               inputStream.close();
           } catch (IOException e) {
               e.printStackTrace();
           }
       }
   }
   return baos;
}

请回答我的问题

0 个答案:

没有答案