Skip to content

Commit

Permalink
set new image provider
Browse files Browse the repository at this point in the history
  • Loading branch information
clagomess committed Mar 7, 2024
1 parent 35bfa4b commit 3d5193e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void build() throws Exception {
throw e;
} finally {
if(bufferHtml.exists()){
pdfParser.addFeatureHTML(Files.newInputStream(bufferHtml.toPath()));
pdfParser.addFeatureHTML(feature, Files.newInputStream(bufferHtml.toPath()));
}

bufferHtml.delete();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import com.github.clagomess.pirilampo.core.dto.ParametersDto;
import com.github.clagomess.pirilampo.core.enums.LayoutPdfEnum;
import com.itextpdf.text.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfTemplate;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.codec.Base64;
import com.itextpdf.tool.xml.XMLWorker;
import com.itextpdf.tool.xml.XMLWorkerFontProvider;
import com.itextpdf.tool.xml.XMLWorkerHelper;
Expand All @@ -18,51 +20,28 @@
import com.itextpdf.tool.xml.pipeline.css.CSSResolver;
import com.itextpdf.tool.xml.pipeline.css.CssResolverPipeline;
import com.itextpdf.tool.xml.pipeline.end.PdfWriterPipeline;
import com.itextpdf.tool.xml.pipeline.html.AbstractImageProvider;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipeline;
import com.itextpdf.tool.xml.pipeline.html.HtmlPipelineContext;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

@Slf4j
@RequiredArgsConstructor
public class PdfParser {
private final ParametersDto parameters;
private final InputStream css;
private final PdfImageProvider pdfImageProvider;

private Document document;
private XMLParser xmlParser;

private static class Base64ImageProvider extends AbstractImageProvider {
@Override
public Image retrieve(String src) {
log.info("- Processing image: {}", src);
/* @TODO: improve performace
int pos = src.indexOf("base64,");
try {
if (src.startsWith("data") && pos > 0) {
byte[] img = Base64.decode(src.substring(pos + 7));
return Image.getInstance(img);
}
else {
return Image.getInstance(src);
}
} catch (BadElementException | IOException ex) {
log.warn(log.getName(), ex);
return null;
}
*/
return null;
}

@Override
public String getImageRootPath() {
return null;
}
public PdfParser(ParametersDto parameters, InputStream css) {
this.parameters = parameters;
this.css = css;
this.pdfImageProvider = new PdfImageProvider(parameters);
}

public void initDocument(
Expand Down Expand Up @@ -91,7 +70,7 @@ public void initDocument(
XMLWorkerFontProvider fontProvider = new XMLWorkerFontProvider(XMLWorkerFontProvider.DONTLOOKFORFONTS);
HtmlPipelineContext htmlContext = new HtmlPipelineContext(new CssAppliersImpl(fontProvider));
htmlContext.setTagFactory(Tags.getHtmlTagProcessorFactory());
htmlContext.setImageProvider(new Base64ImageProvider());
htmlContext.setImageProvider(pdfImageProvider);

// Pipelines
PdfWriterPipeline pdf = new PdfWriterPipeline(document, pw);
Expand All @@ -102,7 +81,8 @@ public void initDocument(
xmlParser = new XMLParser(new XMLWorker(cssP, true));
}

public void addFeatureHTML(InputStream html) throws IOException {
public void addFeatureHTML(File currentFeature, InputStream html) throws IOException {
pdfImageProvider.setCurrentFeature(currentFeature);
xmlParser.parse(html);
}

Expand Down

0 comments on commit 3d5193e

Please sign in to comment.