java web exports data as code snippets in pdf format files
- 2020-06-01 09:49:00
- OfStack
What this snippet does: upon accessing the request, the browser opens a new screen and displays the pdf file preview, which can be downloaded from the file preview screen.
1. jsp interface code
<
input type="button" class="btn btn-info "onclick="getVerPdf();" target="_blank" value=" export as pdf file "/
>
2. js code
function getVerPdf() {
window.open('/pms/jsp/version/getPrdVerListPdf?page='
+ $("#getPage").html() + '&key=' + $("#select").val());
}
3. java code
/**
*
* Purpose : Export the product version list as pdf format
*
* @param req
* request
* @param resp
* The reply
* @param page
* The current number of pages
*/
@RequestMapping(value = "getPrdVerListPdf")
public void getPrdTypeList(HttpServletRequest req, HttpServletResponse resp, Integer page, String key) {
resp.setContentType("application/pdf");
// Select save path and file name
// resp.setHeader("content-disposition",
// "attachment;filename=PrdVerList.pdf");
// Get the data for the current page
List<Version> verList = prdVersionSer.getAllPrdVersion(key);
if (verList.size() == 0) {
// If there is no data, it returns to the main screen and displays a prompt message
req.setAttribute("getFileMsg", " No qualified information! ");
req.setAttribute("select", key);
try {
req.getRequestDispatcher("/jsp/version/ver_list.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// If there is data, display pdf file
JRBeanCollectionDataSource ds = new JRBeanCollectionDataSource(verList);
String reportPath = null;
Map<String, Object> map = new HashMap<String, Object>();
if (key != "") {
map.put("prdName", verList.get(0).getPrdName());
} else {
map.put("prdName", "");
}
reportPath = req.getServletContext().getRealPath("/reports/prdVerListByPrdName.jasper");
InputStream is = null;
try {
is = new FileInputStream(reportPath);
JasperRunManager.runReportToPdfStream(is, resp.getOutputStream(), map, ds);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}