1. Project Clover database Mon Dec 22 2025 14:59:38 CST
  2. Package org.europa.together.application.internal

File PdfReplacedElementFactory.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
89% of files have more coverage

Code metrics

4
22
4
1
85
67
8
0.36
5.5
4
2

Classes

Class Line # Actions
PdfReplacedElementFactory 25 22 0% 8 6
0.880%
 

Contributing tests

This file is covered by 3 tests. .

Source view

1    package org.europa.together.application.internal;
2   
3    import java.io.FileInputStream;
4    import java.io.InputStream;
5    import org.w3c.dom.Element;
6    import org.xhtmlrenderer.extend.FSImage;
7    import org.xhtmlrenderer.extend.ReplacedElement;
8    import org.xhtmlrenderer.extend.ReplacedElementFactory;
9    import org.xhtmlrenderer.extend.UserAgentCallback;
10    import org.xhtmlrenderer.layout.LayoutContext;
11    import org.xhtmlrenderer.pdf.ITextFSImage;
12    import org.xhtmlrenderer.pdf.ITextImageElement;
13    import org.xhtmlrenderer.render.BlockBox;
14    import org.xhtmlrenderer.simple.extend.FormSubmissionListener;
15    import java.io.File;
16    import org.apache.commons.io.IOUtils;
17    import org.europa.together.application.LogbackLogger;
18    import org.europa.together.business.Logger;
19    import org.europa.together.domain.LogLevel;
20    import org.openpdf.text.Image;
21   
22    /**
23    * Helper class to replace elements fürm HTML content to include them in PDF.
24    */
 
25    public class PdfReplacedElementFactory implements ReplacedElementFactory {
26   
27    private static final Logger LOGGER = new LogbackLogger(PdfReplacedElementFactory.class);
28   
29    private int height;
30    private int widht;
31   
 
32  48 toggle @Override
33    public ReplacedElement createReplacedElement(final LayoutContext c, final BlockBox box,
34    final UserAgentCallback uac, final int cssWidth, final int cssHeight) {
35  48 Element e = box.getElement();
36  48 String nodeName = e.getNodeName();
37  48 if (nodeName.equals("img")) {
38  1 String imagePath = e.getAttribute("src");
39  1 InputStream input = null;
40  1 try {
41  1 LOGGER.log("imagePath: " + imagePath, LogLevel.DEBUG);
42  1 LOGGER.log("File exist: " + new File(imagePath).exists(), LogLevel.DEBUG);
43   
44  1 input = new FileInputStream(new File(imagePath));
45  1 byte[] bytes = IOUtils.toByteArray(input);
46  1 input.close();
47  1 FSImage fsImage = new ITextFSImage(
48    Image.getInstance(bytes)
49    );
50  1 LOGGER.log("IMAGE: " + fsImage.toString(), LogLevel.DEBUG);
51   
52  1 if (cssWidth != -1 || cssHeight != -1) {
53  0 this.height = cssWidth;
54  0 this.widht = cssHeight;
55    } else {
56  1 this.height = fsImage.getWidth();
57  1 this.widht = fsImage.getHeight();
58    }
59    // fsImage.scale(this.widht, this.height);
60  1 LOGGER.log("Image Dimension = H:" + this.height + " W:" + this.widht,
61    LogLevel.DEBUG);
62   
63  1 return new ITextImageElement(fsImage);
64    } catch (Exception ex) {
65  0 LOGGER.catchException(ex);
66    }
67    }
68  47 return null;
69    }
70   
 
71  3 toggle @Override
72    public void reset() {
73    // Auto-generated method stub
74    }
75   
 
76  0 toggle @Override
77    public void remove(final Element e) {
78    // Auto-generated method stub
79    }
80   
 
81  0 toggle @Override
82    public void setFormSubmissionListener(final FormSubmissionListener listener) {
83    // Auto-generated method stub
84    }
85    }