1.controller
@ApiOperation(value = "生成清单PDF", notes="发货清单生成PDF") @PostMapping(value = "/createPdf") public void createPdf(@RequestParam("deliveryShipNo") String deliveryShipNo ,HttpServletResponse response) throws Exception {try {itextPdfService.printHandOver(deliveryShipNo, request, response, "");}catch (Exception e){e.printStackTrace();log.error("发货清单生成PDF失败: "+e.getMessage());} }
2.service
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;public interface ItextPdfService {void printHandOver(String deliveryShipNo, HttpServletRequest request, HttpServletResponse response, String userBean) throws Exception;}
3.impl
import lombok.extern.slf4j.Slf4j; import net.sf.jasperreports.engine.*; import net.sf.jasperreports.engine.export.JRPdfExporter; import net.sf.jasperreports.engine.export.JRXlsExporterParameter; import net.sf.jasperreports.engine.fill.JRFileVirtualizer; import net.sf.jasperreports.engine.util.JRLoader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service;import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.File; import java.sql.Connection; import java.util.HashMap; import java.util.Map; @Slf4j @Service public class ItextPdfServiceImpl implements ItextPdfService {@Autowiredprivate JdbcTempBusiness jdbcTempBusiness;@Overridepublic void printHandOver(String deliveryShipNo, HttpServletRequest request, HttpServletResponse response, String userBean) throws Exception {Map<String, Object> mapParameter = new HashMap<String, Object>(2);mapParameter.put("deliveryShipNo", deliveryShipNo);//mapParameter.put("ATTACHMENT_DIR", ReportUtil.getRealPath(request, Constants.TEMPLEMENT_PATH) + "/");mapParameter.put("ATTACHMENT_DIR",Constants.PATH + "/");Connection connection = jdbcTempBusiness.getDataSource().getConnection();try {exportPdf("load_pdf.jasper", mapParameter, request, response, connection);} catch (Exception e) {log.info(e.getMessage());}finally {connection.close();}}public void exportPdf(String jasperFileName, Map<String, Object> parameters, HttpServletRequest request, HttpServletResponse response, Connection conn) throws Exception {//File jasperFile = new File(ReportUtil.getRealPath(request, Constants.TEMPLEMENT_PATH) + "/" + jasperFileName);File jasperFile = new File(Constants.PATH + "/" + jasperFileName);JasperReport jasperReport = (JasperReport) JRLoader.loadObject(jasperFile);parameters.putAll(getVirtualizerParameter(request));JasperPrint jasperPrint= JasperFillManager.fillReport(jasperReport, parameters, conn);exportPdf(jasperPrint, response);}public void exportPdf(JasperPrint jasperPrint, HttpServletResponse response) throws Exception {JRPdfExporter exporter = new JRPdfExporter();try {exporter.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, response.getOutputStream());response.setHeader("Content-Disposition", "inline;filename=report.pdf");response.setContentType("application/pdf");exporter.setParameter(JRExporterParameter.CHARACTER_ENCODING, "UTF-8");exporter.exportReport();} catch (Exception e) {e.printStackTrace();} finally {if(response.getOutputStream()!=null){response.getOutputStream().flush();response.getOutputStream().close();}}}private Map<String, Object> getVirtualizerParameter(HttpServletRequest request) {//String virtualizerFilePath = ReportUtil.getRealPath(request, Constants.VIRTUALIZER_PATH);JRFileVirtualizer virtualizer = new JRFileVirtualizer(400, Constants.PATH); // 一次加载页数Map<String, Object> parameters = new HashMap<String, Object>(1);parameters.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);return parameters;}}
4.JdbcTempBusiness接口
import javax.sql.DataSource;public interface JdbcTempBusiness {DataSource getDataSource(); }
5.JdbcTempBusinessImpl类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Service;import javax.sql.DataSource;@Service public class JdbcTempBusinessImpl implements JdbcTempBusiness {@Autowiredprivate JdbcTemplate jdbcTemplate;public DataSource getDataSource() {return jdbcTemplate.getDataSource();}}
6.pom.xml
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-jdbc</artifactId> </dependency>