iText – HTML to PDF - Image is not displayed in PDF

前端 未结 5 1979
感动是毒
感动是毒 2020-12-31 02:03

I have a html page with text, image and I am parsing the HTML content to iText to generate the PDF. In the generated PDF,Included images are not getting displayed and , only

5条回答
  •  既然无缘
    2020-12-31 02:26

    I think you can do it easily using a Servlet for viewing the image. How to write a servlet for this is here

    Here a sample dispatcher for you. Just edit the required places as needed

    @Controller
    public class ImageController extends DispatcherServlet {
    
    
    
        private static final int DEFAULT_BUFFER_SIZE = 10240; // 10KB.
    
        // Properties ---------------------------------------------------------------------------------
    
        private String imagePath;
    
       @RequestMapping(value="images/{imageId:.+}", method = RequestMethod.GET)
       public @ResponseBody void getImage(@PathVariable String imageId,HttpServletRequest request, HttpServletResponse response){
            String requestedImage = request.getPathInfo();
             this.imagePath ="image path in server here";
    
             if (requestedImage == null) {
                 // Do your thing if the image is not supplied to the request URI.
                 // Throw an exception, or send 404, or show default/warning image, or just ignore it.
                 try {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
                 }catch(IOException ioException){
                    logger.error("error image path incorrect:{}", ioException);
    
                } // 404.
                 return;
             }
    
             File image=null;
            try {
                image = new File(imagePath, URLDecoder.decode(imageId, "UTF-8"));
            } catch (UnsupportedEncodingException unsupportedEncodingException) {
                logger.error("error image can not decode:{}", unsupportedEncodingException);
    
            }
    
             // Check if file actually exists in filesystem.
             if (!image.exists()) {
                 // Do your thing if the file appears to be non-existing.
                 // Throw an exception, or send 404, or show default/warning image, or just ignore it.
                 try {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
                 }catch(IOException ioException){
                    logger.error("error image does not exists:{}", ioException);
    
                } // 404.
                 return;
             }
    
             // Get content type by filename.
             String contentType = "jpeg";
             contentType="image/"+contentType;
    
             // Init servlet response.
             response.reset();
             response.setBufferSize(DEFAULT_BUFFER_SIZE);
             response.setContentType(contentType);
             response.setHeader("Content-Length", String.valueOf(image.length()));
             response.setHeader("Content-Disposition", "inline; filename=\"" + image.getName() + "\"");
    
             // Prepare streams.
             BufferedInputStream input = null;
             BufferedOutputStream output = null;
    
             try {
                 // Open streams.
                 try {
                    input = new BufferedInputStream(new FileInputStream(image), DEFAULT_BUFFER_SIZE);
                } catch (FileNotFoundException e) {
    
                    logger.error("error creating file input stream to the image file :{}", e);
    
    
                }
                 try {
    
                     output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
    
                } catch (IOException e) {
    
    
                    logger.error("error creating output stream to the http response :{}", e);
    
                }
    
                 // Write file contents to response.
                 byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
                 int length;
                 try {
                    while ((length = input.read(buffer)) > 0) {
                         output.write(buffer, 0, length);
                     }
                } catch (IOException e) {
    
                    logger.error("error writing the image file to outputstream :{}", e);
    
                }
             } finally {
                 // Gently close streams.
                 close(output);
                 close(input);
             }
         }
    
         // Helpers (can be refactored to public utility class) ----------------------------------------
    
    
    
    
    private  void close(Closeable resource) {
        if (resource != null) {
            try {
                resource.close();
            } catch (IOException e) {
                // Do your thing with the exception. Print it, log it or mail it.
                logger.error("error closing resources:{}", e);
            }
        }
    }
    
    
    
    
    }
    

提交回复
热议问题