【问题标题】:Vaadin Convert and display image as PDFVaadin 将图像转换并显示为 PDF
【发布时间】:2016-10-20 11:01:17
【问题描述】:

有谁知道如何将图像文件轻松转换为 PDF 格式。我需要的是从数据库中获取图像并将其以 PDF 格式显示在屏幕上。我究竟做错了什么?我尝试使用 iText 但没有结果。 我的代码:

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

谢谢。

【问题讨论】:

  • 分两步拆分。 PDF 是否正确生成?显示它。 (但为什么要通过PDF显示图像,直接显示图像会更高效
  • 为什么要将图像转换为 PDF?这将要求某些用户安装 PDF 程序。确定将图像显示为图像可以在任何浏览器中工作吗?
  • 克里斯,当我使用 Vaadin 显示图像时,问题出在 tt - 它的大小(比例)始终取决于浏览器。 Firefox 在窗口宽度内呈现图像,而 Chrome 显示相同的图像比屏幕尺寸更大更宽。但我需要确保 tt 图像始终以屏幕全宽显示,并且不会被浏览器剪切。
  • 感谢@Lena,这是有道理的。您可以尝试使用 CSS 来正确显示图像,或者甚至通过将图像添加到带有 HTML 的 CustomLayout 中,以按照您想要的方式显示图像。

标签: java pdf itext vaadin image-conversion


【解决方案1】:

您没有正确使用 iText:

  1. 您永远不会关闭写入器,因此图像的添加永远不会写入输出流。

  2. 您将一个空字符串传递给您的FileOutputStream。如果要将 pdf 保存在内存中,请使用 ByteArrayOutputStream。如果没有,请定义一个临时名称。

  3. 您将 Document 对象(一个 iText 特定对象)传递给您的 Embedded 对象,并将其视为文件。它不是 pdf 文件或字节 []。您可能希望传递您的 ByteArrayOutputStream 或将临时文件作为 ByteArrayOutputStream 读入内存并将其传递给 Embedded

【讨论】:

    【解决方案2】:

    也许有人会使用(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;
    }
    

    【讨论】:

      猜你喜欢
      • 2010-09-09
      • 1970-01-01
      • 2012-03-10
      • 2013-07-03
      • 2016-07-30
      • 1970-01-01
      • 2019-12-04
      • 2011-09-28
      • 1970-01-01
      相关资源
      最近更新 更多