Commit 150f1f0c authored by 刘学辉's avatar 刘学辉

打印PDF调整为公共方法

parent 4e36e722
...@@ -151,7 +151,7 @@ function reloadGrid() { ...@@ -151,7 +151,7 @@ function reloadGrid() {
} }
function printPage() { function printPage() {
Public.openPostWindow(web_app.name + '/PDFViewer/PDFViewer.do',{id: $("#id").val(),fileType:"permit"}); Public.openPostWindow(web_app.name + '/PDFViewer/PDFViewer.do',{id: $("#id").val(),applicationType:"sapPermit"});
} }
function getExtendedData(processAction) { function getExtendedData(processAction) {
......
...@@ -14,7 +14,7 @@ ...@@ -14,7 +14,7 @@
<script type="text/javascript"> <script type="text/javascript">
$(function () { $(function () {
$("#pdfViewer").attr("height", $(document).height()); $("#pdfViewer").attr("height", $(document).height());
var url = "${basePath}/pdfjs/web/viewer.html?file=" + encodeURIComponent("${basePath}/PDFViewer/displayPDF.ajax?id=${fileId}&fileType=${fileType}"); var url = "${basePath}/pdfjs/web/viewer.html?file=" + encodeURIComponent("${basePath}/PDFViewer/displayPDF.ajax?id=${fileId}&applicationType=${applicationType}");
$("#pdfViewer").attr("src", url); $("#pdfViewer").attr("src", url);
}) })
</script> </script>
......
...@@ -69,7 +69,7 @@ ...@@ -69,7 +69,7 @@
<span style="font-family:SimSun;">${(list.belongings)!''}</span> <span style="font-family:SimSun;">${(list.belongings)!''}</span>
</p> </p>
</td> </td>
<td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; vertical-align:top; width:80pt"> <td style="border-bottom-color:#000000; border-bottom-style:solid; border-bottom-width:0.75pt; border-right-color:#000000; border-right-style:solid; border-right-width:0.75pt; border-top-color:#000000; border-top-style:solid; border-top-width:0.75pt; vertical-align:top; width:60pt">
<p style="margin:0pt; orphans:0; widows:0"> <p style="margin:0pt; orphans:0; widows:0">
<span style="font-family:SimSun;">${(list.quantity)!''}</span> <span style="font-family:SimSun;">${(list.quantity)!''}</span>
</p> </p>
......
...@@ -56,7 +56,7 @@ public class CommonUtils { ...@@ -56,7 +56,7 @@ public class CommonUtils {
// module宽度 UnitConv.in2mm(1.f / dpi); // module宽度 UnitConv.in2mm(1.f / dpi);
double moduleWidth = 0.4; double moduleWidth = 0.4;
bean.setModuleWidth(moduleWidth); bean.setModuleWidth(moduleWidth);
bean.setMsgPosition(HumanReadablePlacement.HRP_NONE); bean.setMsgPosition(HumanReadablePlacement.HRP_BOTTOM);
bean.setHeight(10); bean.setHeight(10);
bean.doQuietZone(false); bean.doQuietZone(false);
...@@ -69,7 +69,7 @@ public class CommonUtils { ...@@ -69,7 +69,7 @@ public class CommonUtils {
public static File genBarCode128(String msg, String imgType, File file) public static File genBarCode128(String msg, String imgType, File file)
throws IOException { throws IOException {
FileOutputStream out = new FileOutputStream(file); FileOutputStream out = new FileOutputStream(file);
try { try {
generateBarCode128(msg,imgType, out); generateBarCode128(msg,imgType, out);
} finally { } finally {
......
...@@ -8,41 +8,41 @@ import com.huigou.uasp.bmp.doc.attachment.application.AttachmentApplication; ...@@ -8,41 +8,41 @@ import com.huigou.uasp.bmp.doc.attachment.application.AttachmentApplication;
import com.huigou.uasp.bmp.doc.attachment.domain.model.Attachment; import com.huigou.uasp.bmp.doc.attachment.domain.model.Attachment;
import com.huigou.uasp.client.CommonController; import com.huigou.uasp.client.CommonController;
import com.huigou.util.ApplicationContextWrapper;
import com.huigou.util.SDO; import com.huigou.util.SDO;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.web.context.WebApplicationContext;
import javax.servlet.ServletOutputStream; import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.*; import java.io.*;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
@ControllerMapping("/PDFViewer") @ControllerMapping("/PDFViewer")
@Controller @Controller
public class PDFViewerController extends CommonController { public class PDFViewerController extends CommonController {
@Autowired
private AttachmentApplication attachmentApplication;
// @Autowired
// private AttachmentContentResolverComposition attachmentContentResolverComposition;
@Autowired
private SapPermitApplication sapPermitApplication;
@Override @Override
public String getPagePath() { public String getPagePath() {
return "/pdfjs/"; return "/pdfjs/";
} }
@SkipAuth @SkipAuth
public String displayPDF() { public String displayPDF() {
SDO sdo = this.getSDO(); SDO sdo = this.getSDO();
String id = sdo.getString("id"); String id = sdo.getString("id");
String fileType = sdo.getString("fileType"); String applicationType = sdo.getString("applicationType");
try { try {
ByteArrayOutputStream byteArrayOutputStream=null; ByteArrayOutputStream byteArrayOutputStream=null;
if ("permit".equals(fileType)){ Object object=ApplicationContextWrapper.getBean(applicationType+"Application");
byteArrayOutputStream = sapPermitApplication.printPermitInfo(this.getResponse(), id); Method method = object.getClass().getMethod("printInfo", HttpServletResponse.class, String.class);
} byteArrayOutputStream =(ByteArrayOutputStream)method.invoke(object, this.getResponse(), id);
InputStream in=new ByteArrayInputStream(byteArrayOutputStream.toByteArray()); InputStream in=new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
HttpServletResponse response=this.getResponse(); HttpServletResponse response=this.getResponse();
ServletOutputStream out = response.getOutputStream(); ServletOutputStream out = response.getOutputStream();
...@@ -53,30 +53,16 @@ public class PDFViewerController extends CommonController { ...@@ -53,30 +53,16 @@ public class PDFViewerController extends CommonController {
return null; return null;
} }
public static void inputStream2File(InputStream is, File file) throws IOException {
OutputStream os = null;
try {
os = new FileOutputStream(file);
int len = 0;
byte[] buffer = new byte[8192];
while ((len = is.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} finally {
os.close();
is.close();
}
}
public String PDFViewer() { public String PDFViewer() {
SDO sdo = this.getSDO(); SDO sdo = this.getSDO();
String id = sdo.getString("id"); String id = sdo.getString("id");
String fileType = sdo.getString("fileType"); String applicationType = sdo.getString("applicationType");
// String fileName = "放行条";
putAttribute("id", id); putAttribute("id", id);
putAttribute("fileId", id); putAttribute("fileId", id);
putAttribute("fileType", fileType); putAttribute("applicationType", applicationType);
return forward("PDFViewer"); // 跳转预览pdf页面 return forward("PDFViewer"); // 跳转预览pdf页面
} }
} }
\ No newline at end of file
...@@ -32,5 +32,5 @@ public interface SapPermitApplication { ...@@ -32,5 +32,5 @@ public interface SapPermitApplication {
void saveSapPermitVo(SapPermitVo sapPermitVo); void saveSapPermitVo(SapPermitVo sapPermitVo);
ByteArrayOutputStream printPermitInfo(HttpServletResponse response, String id); ByteArrayOutputStream printInfo(HttpServletResponse response, String id);
} }
package com.huigou.topsun.sap.permit.application.impl; package com.huigou.topsun.sap.permit.application.impl;
import cn.hutool.core.bean.BeanUtil; import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSONArray;
import com.huigou.cache.DictUtil; import com.huigou.cache.DictUtil;
import com.huigou.context.Operator; import com.huigou.context.Operator;
import com.huigou.context.OrgUnit; import com.huigou.context.OrgUnit;
...@@ -29,24 +29,19 @@ import com.itextpdf.text.*; ...@@ -29,24 +29,19 @@ import com.itextpdf.text.*;
import com.itextpdf.text.html.simpleparser.HTMLWorker; import com.itextpdf.text.html.simpleparser.HTMLWorker;
import com.itextpdf.text.pdf.*; import com.itextpdf.text.pdf.*;
import com.itextpdf.text.pdf.draw.LineSeparator;
import freemarker.template.Template; import freemarker.template.Template;
import org.activiti.engine.delegate.DelegateExecution; import org.activiti.engine.delegate.DelegateExecution;
import org.activiti.engine.delegate.DelegateTask; import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.runtime.ProcessInstance;
import org.apache.commons.io.IOUtils;
import org.krysalis.barcode4j.HumanReadablePlacement;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils; import org.springframework.util.CollectionUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.xhtmlrenderer.pdf.ITextRenderer;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.*; import java.io.*;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -241,26 +236,33 @@ public class SapPermitApplicationImpl extends FlowBroker implements SapPermitApp ...@@ -241,26 +236,33 @@ public class SapPermitApplicationImpl extends FlowBroker implements SapPermitApp
return localSdo; return localSdo;
} }
public ByteArrayOutputStream printPermitInfo(HttpServletResponse response, String id) { public ByteArrayOutputStream printInfo(HttpServletResponse response, String id) {
// wantGoods.setReceiveAddress(newAddress+wantGoods.getReceiveAddress());
SapPermit permit=this.sapPermitRepository.findOne(id); SapPermit permit=this.sapPermitRepository.findOne(id);
List <SapPermitItem> itemList=this.sapPermitItemRepository.findBySapPermitId(id); List <SapPermitItem> itemList=this.sapPermitItemRepository.findBySapPermitId(id);
Map<String ,Object> data = new HashMap<>(); Map<String ,Object> data = new HashMap<>();
data.put("head",permit); data.put("head",permit);
data.put("itemList",itemList); data.put("itemList",itemList);
//data.put("invoiceList",wantGoods.getInvoiceUsedList());
Document document = null; //Document document = null;
PdfCopy copy; ByteArrayOutputStream byteArrayOutputStream = null;
ByteArrayOutputStream byteArrayOutputStream = null;
try { try {
response.reset(); response.reset();
response.setHeader("Content-Type", "application/pdf-stream"); response.setHeader("Content-Type", "application/pdf-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("放行条.pdf", "UTF-8")); response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("放行条.pdf", "UTF-8"));
String randoms=CommonUtil.createGUID().substring(0,10); String randoms=CommonUtil.createGUID().substring(0,10);
File file = new File("bar-"+randoms+".png"); /*File fileDir=new File("bar");
String IMG_TYPE_PNG="image/png"; if(!fileDir.exists()) {
data.put("barCode", CommonUtils.genBarCode128(permit.getBillCode(),IMG_TYPE_PNG,file )); fileDir.mkdirs();
}*/
File file = new File("bar-"+permit.getBillCode()+".png");
if(!file.exists()) {
String IMG_TYPE_PNG = "image/png";
data.put("barCode", CommonUtils.genBarCode128(permit.getBillCode(), IMG_TYPE_PNG, file));
} else {
data.put("barCode",file);
}
File fileb = new File("baoshen.png"); File fileb = new File("baoshen.png");
data.put("baoshen",fileb); data.put("baoshen",fileb);
Template template = freemarkerConfigurer.getConfiguration().getTemplate("fxt.ftl"); Template template = freemarkerConfigurer.getConfiguration().getTemplate("fxt.ftl");
...@@ -270,9 +272,7 @@ public class SapPermitApplicationImpl extends FlowBroker implements SapPermitApp ...@@ -270,9 +272,7 @@ public class SapPermitApplicationImpl extends FlowBroker implements SapPermitApp
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException("PDF生成失败!"); throw new RuntimeException("PDF生成失败!");
} finally { } finally {
if (document != null) {
document.close();
}
} }
return byteArrayOutputStream; return byteArrayOutputStream;
} }
......
...@@ -97,7 +97,7 @@ public class SapPermitController extends CommonController { ...@@ -97,7 +97,7 @@ public class SapPermitController extends CommonController {
System.out.println("id======="+id); System.out.println("id======="+id);
// HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse(); // HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
HttpServletResponse response = this .getResponse(); HttpServletResponse response = this .getResponse();
sapPermitApplication.printPermitInfo(response,id); sapPermitApplication.printInfo(response,id);
//SapPermit permit=this.sapPermitApplication.getPermitById(id); //SapPermit permit=this.sapPermitApplication.getPermitById(id);
// List <SapPermitItem> itemList=sapPermitItemApplication.findBySapPermitId(id); // List <SapPermitItem> itemList=sapPermitItemApplication.findBySapPermitId(id);
// String template="fxt.ftl"; // String template="fxt.ftl";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment