0

Does anyone know how image file can be easily converted into PDF format. What I need is to get the image from database and display it on the screen as PDF. What am I doing wrong? I tried to use iText but with no results. My code:

StreamResource resource = file.downloadFromDatabase();//get file from db
Document converToPdf=new Document();//Create Document Object  
PdfWriter.getInstance(convertToPdf, new FileOutputStream(""));//Create PdfWriter for Document to hold physical file 
convertToPdf.open();
Image convertJpg=Image.getInstance(resource); //Get the input image to Convert to PDF
convertToPdf.add(convertJpg);//Add image to Document
Embedded pdf = new Embedded("", convertToPdf);//display document
pdf.setMimeType("application/pdf");
pdf.setType(Embedded.TYPE_BROWSER);
pdf.setSizeFull();

Thanks.

Lena
  • 99
  • 4
  • 14
  • 2
    Split it in two steps. Is the PDF generated correctly? The display it. (But why go via PDF to display a image, display the image directly would be more efficient – André Schild Oct 20 '16 at 11:17
  • Why would you want to convert and image into a PDF? That would require the some users to have a PDF program installed. Surely displaying the image as an image will work in any browser? – Chris M Oct 20 '16 at 15:46
  • Chris, the issue is tt when I display the image with Vaadin - its size (scale) is always browser- dependent. Firefox renders the image within the window width, while Chrome shows the same image as bigger and wider than the screen size. But I need to be sure tt image is always displayed in full width of the screen and is not cut by browser. – Lena Oct 20 '16 at 17:53
  • Thanks @Lena that makes sense. You could try getting the image to show correctly using CSS instead, or even by adding the image into a CustomLayout with HTML that will display the image how you want it. – Chris M Oct 21 '16 at 12:58

2 Answers2

1

You're not using iText correctly:

  1. You never close your writer, so the addition of the image never gets written to the outputstream.

  2. You pass an empty string to your FileOutputStream. If you want to keep the pdf in memory, use a ByteArrayOutputStream. If not, define a temporary name instead.

  3. You pass your Document object, which is a iText-specific object to your Embedded object and treat it like a file. It is not a pdf-file or byte[]. You'll probably want to pass either your ByteArrayOutputStream or read the temp file as a ByteArrayOutputStream into memory and pass that to Embedded.

Samuel Huylebroeck
  • 1,639
  • 11
  • 15
0

Maybe someone will use (Vaadin + iText)

Button but = new Button("FV");

    StreamResource myResource = getPDFStream();
    FileDownloader fileDownloader = new FileDownloader(myResource);
    fileDownloader.extend(but);

    hboxBottom.addComponent( but );


private StreamResource getPDFStream() {
    StreamResource.StreamSource source = new StreamResource.StreamSource() {

        public InputStream getStream() {

            // step 1
        com.itextpdf.text.Document document = new com.itextpdf.text.Document();
        // step 2
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

            try {
                com.itextpdf.text.pdf.PdfWriter.getInstance(document, baos);

                // step 3
                document.open();

                document.add(Chunk.NEWLINE);   //Something like in HTML :-)

                document.add(new Paragraph("TEST" ));


                document.add(Chunk.NEWLINE);   //Something like in HTML :-)                             

                document.newPage();            //Opened new page

                //document.add(list);            //In the new page we are going to add list

                document.close();

                //file.close();

                System.out.println("Pdf created successfully..");




            } catch (DocumentException ex) {
                Logger.getLogger(WndOrderZwd.class.getName()).log(Level.SEVERE, null, ex);
            }

            ByteArrayOutputStream stream = baos;
            InputStream input = new ByteArrayInputStream(stream.toByteArray());
              return input;

        }
    };
  StreamResource resource = new StreamResource ( source, "test.pdf" );
    return resource;
}