1

I need to convert certain data to a pdf file. For this I have wriitten the following code which will save data in TablePdf.pdf in the server. (Here the pdf file is saved in C:\ directory)

public String generatePdf() throws Exception
{
Font font = FontFactory.getFont("Ms Dialog Light");
BaseFont pdfFont = font.getBaseFont();

// TODO Auto-generated method stub
HashMap inputMap = new HashMap();

inputMap.put(TableProperties.PDF_PATH, "c://TablePdf.pdf");
inputMap.put(TableProperties.PDF_TABLE_NAME, "Table");

inputMap.put(TableProperties.PDF_HEIGHT, "1000");
inputMap.put(TableProperties.PDF_WIDTH, "1500");

ArrayList<String> columnNameList = new ArrayList<String>();
ArrayList<String> dataList = new ArrayList<String>();
ArrayList<String> columnWidthList = new ArrayList<String>();

columnNameList.add("Col1");
columnNameList.add("Col2");
columnNameList.add("Col3");
columnNameList.add("Col4");
columnNameList.add("Col5");

columnWidthList.add("1");
columnWidthList.add("2");
columnWidthList.add("2");
columnWidthList.add("3");
columnWidthList.add("1");

for (int i = 0; i < 9; i++)
    {
    dataList.add("Id" + i);
    dataList.add("Name is = " + Math.random() * i);
    dataList.add("Field Value1 is = " + Math.random() * i);
    dataList.add("Field Value2 is = " + Math.random() * i);
    dataList.add("Field Value3 is = " + Math.random() * i);
    }

inputMap.put(TableProperties.PDF_TABLE_COLUMN_NUMBER, "5");
inputMap.put(TableProperties.PDF_TABLE_COLUMN_NAME, columnNameList);
inputMap.put(TableProperties.PDF_TABLE_COLUMN_VALUES, dataList);

inputMap.put(TableProperties.PDF_TABLE_HEADER_WIDTH, columnWidthList);

inputMap.put(TableProperties.PDF_HEADER, "            Hello\n\n");
inputMap.put(TableProperties.PDF_HEADER_FONT_NAME, pdfFont);
inputMap.put(TableProperties.PDF_HEADER_FONT_SIZE, "20.0");
inputMap.put(TableProperties.PDF_HEADER_ALIGNMENT, Element.ALIGN_LEFT);

inputMap.put(TableProperties.PDF_FOOTER, "             Tata");
inputMap.put(TableProperties.PDF_FOOTER_FONT_NAME, pdfFont);
inputMap.put(TableProperties.PDF_FOOTER_FONT_SIZE, "9.0");
inputMap.put(TableProperties.PDF_FOOTER_ALIGNMENT, Element.ALIGN_RIGHT);

inputMap.put(TableProperties.PDF_TABLE_CELL_HEIGHT, "6.0");
inputMap.put(TableProperties.PDF_TABLE_HEADER_HEIGHT, "4.0");

inputMap.put(TableProperties.PDF_TABLE_ALTERNATE_BACKGROUND_COLOR, "Y");
inputMap.put(TableProperties.PDF_TABLE_BACKGROUND_COLOR, BaseColor.CYAN);
inputMap.put(TableProperties.PDF_TABLE_CELL_ALIGNMENT, new Integer(Element.ALIGN_LEFT));
inputMap.put(TableProperties.PDF_TABLE_FONT_NAME, pdfFont);
inputMap.put(TableProperties.PDF_TABLE_FONT_SIZE, "6.0");
inputMap.put(TableProperties.PDF_TABLE_HEADER_ALIGNMENT, new Integer(Element.ALIGN_CENTER));
inputMap.put(TableProperties.PDF_TABLE_HEADER_BACKGROUND_COLOR, BaseColor.GRAY);

inputMap.put(TableProperties.PDF_TABLE_HEADER_FONT_NAME, FontFactory.getFont("Times-Roman").getBaseFont());
inputMap.put(TableProperties.PDF_TABLE_HEADER_FONT_SIZE, "6.0");

CreateTable crtTbl = new CreateTable();
    crtTbl.createTable(inputMap);
}

Now I need to allow the client so that they can download the pdf file.

--------------------EDITED--------------------------------

Below is my jsp code to download the pdf file. Its giving no error in the console, but the file is not downloading.

<%@ page import="java.util.*,java.io.*"%>
<%@ page language="java"%>

<%
    try
    {

        response.setContentType ("application/pdf");    
        //set the header and also the Name by which user will be prompted to save
        response.setHeader ("Content-Disposition", "attachment;filename=TablePdf.pdf");

        File f = new File ("C:\\TablePdf.pdf");

        InputStream inputStream = new FileInputStream(f);
        ServletOutputStream servletOutputStream = response.getOutputStream();
        int bit = 256;
        int i = 0;
        try 
        {

            while ((bit) >= 0) 
            {
                bit = inputStream.read();
                servletOutputStream.write(bit);
            }
            System.out.println("" +bit);


            }
            catch (Exception ioe) 
            {
                ioe.printStackTrace(System.out);
            }
            servletOutputStream.flush();
            //outs.close();
            inputStream.close();    
    }
    catch(Exception e)
    {

    }

%>
user2551629
  • 41
  • 1
  • 6
  • What is your server and what could be the clients ? btw the hole code you have posted here is not relevant for answering the question. – A4L Jul 17 '13 at 10:15

1 Answers1

2

There are many options. Two of them:

  • Install a simple Apache server - you store the PDF files under htdocs, and they will be accessible
  • Have tomcat (or another servlet container), and make a servlet that reads files from the directory they are stored and streams them for download. In short, this is done by transferring their bytes from the FileInputStream to the response.getOutputStream(). Also set the Content-Disposition` header accordingly
Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140