org.apache.pdfbox.exceptions.COSVisitorException Java Examples

The following examples show how to use org.apache.pdfbox.exceptions.COSVisitorException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ReportConvertPdf.java    From yuzhouwan with Apache License 2.0 4 votes vote down vote up
private static void finish(String pdfPath, PDDocument doc, PDPageContentStream content)
        throws IOException, COSVisitorException {
    content.close();
    doc.save(pdfPath);
    doc.close();
}
 
Example #2
Source File: ReportGenUtil.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Get microgateway request summary report as a pdf.
 * @param username
 * @param date month with year. Format should be yyyy-mm (ex: 2018-07)
 * @return inputstream InputStream of the pdf
 * @throws APIManagementException exception
 */
public static InputStream getMicroGatewayRequestSummaryReport(String username, String date)
        throws APIManagementException {
    InputStream pdfInputStream = null;
    // get data
    String value = "0"; //default 
    String appName = "APIM_ACCESS_SUMMARY";
    String query = "from ApiUserPerAppAgg on gatewayType=='MICRO' within '" + date 
            + "-** **:**:**' per 'months' SELECT sum(totalRequestCount) as sum;";
    
    JSONObject jsonObj = APIUtil.executeQueryOnStreamProcessor(appName, query);
    JSONArray jarray =  (JSONArray) jsonObj.get("records");
    if(jarray != null && jarray.size() != 0) {
        JSONArray val = (JSONArray) jarray.get(0);
        Long result = (Long) val.get(0);
        value = String.valueOf(result);
    }

    //build data object to pass for generation
    TableData table = new TableData();
    String[] columnHeaders = { "", "Date", "Number of requests" };
    table.setColumnHeaders(columnHeaders);
    
    List<RowEntry> rowData = new ArrayList<RowEntry>();
    RowEntry entry = new RowEntry();
    entry.setEntry("1");
    entry.setEntry(date);
    entry.setEntry(value);
    rowData.add(entry);
    table.setRows(rowData);

    // generate the pdf
    ReportGenerator generator = new ReportGenerator();

    try {
        pdfInputStream =  generator.generateMGRequestSummeryPDF(table);
    } catch (COSVisitorException | IOException e) {
        String msg = "Error while generating the pdf for micro gateway request summary";
        log.error(msg, e);
        throw new APIManagementException(msg, e);
    }
    return pdfInputStream;
}
 
Example #3
Source File: ReportGenerator.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
/**
 * Generate PDF file for API microgateway request summary
 *
 * @param table object containing table headers and row data
 * @return InputStream pdf as a stream
 * @throws IOException
 * @throws COSVisitorException
 */
public InputStream generateMGRequestSummeryPDF(TableData table) throws IOException, COSVisitorException {

    String[] columnHeaders = table.getColumnHeaders();

    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    page.setMediaBox(PDPage.PAGE_SIZE_A4);
    page.setRotation(0);
    document.addPage(page);

    PDPageContentStream contentStream = new PDPageContentStream(document, page, false, false);

    // add logo
    InputStream in = APIManagerComponent.class.getResourceAsStream("/report/wso2-logo.jpg");
    PDJpeg img = new PDJpeg(document, in);
    contentStream.drawImage(img, 375, 755);

    // Add topic
    contentStream.setFont(PDType1Font.HELVETICA_BOLD, 16);
    writeContent(contentStream, CELL_MARGIN, 770, "API Microgateway request summary");

    // Add generated time
    contentStream.setFont(PDType1Font.HELVETICA_BOLD, FONT_SIZE);
    writeContent(contentStream, CELL_MARGIN, 730, "Report generated on: " + new Date().toString());

    contentStream.setFont(TEXT_FONT, FONT_SIZE);

    // add table with data
    drowTableGrid(contentStream, table.getRows().size());
    writeRowsContent(contentStream, columnHeaders, table.getRows());

    // Add meta data
    // Whenever the summary report structure is updated this should be changed
    String requestCount = table.getRows().get(0).getEntries().get(2);
    document.getDocumentInformation().setCustomMetadataValue(MGW_META, getMetaCount(requestCount));

    contentStream.close();
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    document.save(out);
    document.close();

    return new ByteArrayInputStream(out.toByteArray());

}