Example usage for org.springframework.http MediaType APPLICATION_PDF_VALUE

List of usage examples for org.springframework.http MediaType APPLICATION_PDF_VALUE

Introduction

In this page you can find the example usage for org.springframework.http MediaType APPLICATION_PDF_VALUE.

Prototype

String APPLICATION_PDF_VALUE

To view the source code for org.springframework.http MediaType APPLICATION_PDF_VALUE.

Click Source Link

Document

A String equivalent of MediaType#APPLICATION_PDF .

Usage

From source file:fr.cph.stock.controller.portfolio.PortfolioController.java

@RequestMapping(value = "/pdf", method = RequestMethod.GET, produces = MediaType.APPLICATION_PDF_VALUE)
public void pdf(final HttpServletResponse response,
        @RequestParam(value = FROM, required = false) @DateTimeFormat(pattern = DATE_FORMAT) final Date fromDate,
        @RequestParam(value = TO, required = false) @DateTimeFormat(pattern = DATE_FORMAT) final Date toDate,
        @Valid @ModelAttribute final User user, @CookieValue(LANGUAGE) final String lang)
        throws ServletException, ParseException, IOException {
    final Portfolio portfolio = userService.getUserPortfolio(user.getId())
            .orElseThrow(() -> new NotFoundException(user.getId()));
    final Image sectorChart = PdfReport.createPieChart((PieChart) portfolio.getPieChartSector(),
            "Sector Chart");
    final Image capChart = PdfReport.createPieChart((PieChart) portfolio.getPieChartCap(), "Cap Chart");
    final Image timeChart = PdfReport.createTimeChart((TimeChart) portfolio.getTimeChart(), "Share value");
    final PdfReport pdf = new PdfReport(appProperties.getReport().getIreport());
    pdf.addParam(PORTFOLIO, portfolio);//w  ww .  j a  v a 2 s.  c o  m
    pdf.addParam(EQUITIES, portfolio.getEquities());
    pdf.addParam(USER, user);
    pdf.addParam(SECTOR_PIE, sectorChart);
    pdf.addParam(CAP_PIE, capChart);
    pdf.addParam(SHARE_VALUE_PIE, timeChart);
    final DateFormat df = new SimpleDateFormat("dd-MM-yy");
    final String formattedDate = df.format(new Date());
    response.addHeader("Content-Disposition",
            "attachment; filename=" + user.getLogin() + formattedDate + ".pdf");
    try (final OutputStream responseOutputStream = response.getOutputStream()) {
        JasperExportManager.exportReportToPdfStream(pdf.getReport(), responseOutputStream);
    } catch (final JRException e) {
        throw new ServletException("Error: " + e.getMessage(), e);
    }
}

From source file:sg.ncl.MainController.java

@RequestMapping(value = "/orderform/download", method = RequestMethod.GET)
public void OrderForm_v1Download(HttpServletResponse response) throws OrderFormDownloadException, IOException {
    InputStream stream = null;//  ww w .j a va  2s. co m
    response.setContentType(MediaType.APPLICATION_PDF_VALUE);
    try {
        stream = getClass().getClassLoader().getResourceAsStream("downloads/order_form.pdf");
        response.setContentType(APPLICATION_FORCE_DOWNLOAD);
        response.setHeader(CONTENT_DISPOSITION, "attachment; filename=order_form.pdf");
        IOUtils.copy(stream, response.getOutputStream());
        response.flushBuffer();
    } catch (IOException ex) {
        log.info("Error for download orderform.");
        throw new OrderFormDownloadException("Error for download orderform.");
    } finally {
        if (stream != null) {
            stream.close();
        }
    }

}