2016-10-20 20 views
0

画像ファイルをPDF形式に簡単に変換する方法を知っている人はいますか?私が必要とするのは、データベースからイメージを取得し、PDFとして画面に表示することです。私は間違って何をしていますか?私はiTextを使用しようとしましたが結果はありませんでした。 マイコード:Vaadin画像をPDF形式に変換して表示する

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(); 

ありがとうございました。

+2

スプリット、それを使用します。 PDFは正しく生成されていますか?それを表示します。 (しかし、なぜ画像を表示するためにPDFを介して直接画像を表示する方が効率的でしょうか? –

+0

なぜあなたはPDFに変換して画像を作成したいのですか?その場合、PDFプログラムをインストールする必要があります。画像はどのブラウザでも使えますか? –

+0

Chrisは、Vaadinで画像を表示すると問題が発生します - サイズは常にブラウザに依存します.Firefoxは画像をウィンドウ幅内にレンダリングしますが、Chromeは画面のサイズよりも大きくて幅が広いですが、私は必ず画像の全幅に表示され、ブラウザでは切り取られていないことを確認する必要があります。 – Lena

答えて

1

あなたは正しくiTextのを使用していない:

  1. をあなたのライターを閉じることはありませんので、画像の追加が出力ストリームに書き込まれることは決してありません。

  2. FileOutputStreamに空の文字列を渡します。 pdfをメモリに保存したい場合は、ByteArrayOutputStreamを使用してください。そうでない場合は、代わりに一時的な名前を定義します。

  3. オブジェクトにiText固有のオブジェクトであるDocumentオブジェクトを渡し、ファイルのように扱います。 pdfファイルやバイト[]ではありません。 ByteArrayOutputStreamのいずれかを渡すか、ByteArrayOutputStreamという一時ファイルをメモリに読み込んで、それをEmbeddedに渡すことをお勧めします。

0

はたぶん誰かが二段階で(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; 
} 
関連する問題