Skip to content

Commit b267f90

Browse files
committed
Support event meta info
DEVSIX-1958
1 parent b7e2870 commit b267f90

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

src/main/java/com/itextpdf/html2pdf/ConverterProperties.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ This file is part of the iText (R) project.
4747
import com.itextpdf.html2pdf.attach.impl.OutlineHandler;
4848
import com.itextpdf.html2pdf.css.apply.ICssApplierFactory;
4949
import com.itextpdf.html2pdf.css.media.MediaDeviceDescription;
50+
import com.itextpdf.kernel.counter.event.IMetaInfo;
5051
import com.itextpdf.layout.font.FontProvider;
5152

5253
/**
@@ -81,6 +82,9 @@ public class ConverterProperties {
8182
/** Indicates whether the document should be opened in immediate flush or not **/
8283
private boolean immediateFlush = true;
8384

85+
/** Meta info that will be added to the events thrown by html2Pdf */
86+
private IMetaInfo metaInfo;
87+
8488
/**
8589
* Instantiates a new {@link ConverterProperties} instance.
8690
*/
@@ -102,6 +106,7 @@ public ConverterProperties(ConverterProperties other) {
102106
this.createAcroForm = other.createAcroForm;
103107
this.outlineHandler = other.outlineHandler;
104108
this.charset = other.charset;
109+
this.metaInfo = other.metaInfo;
105110
}
106111

107112
/**
@@ -289,4 +294,26 @@ public ConverterProperties setImmediateFlush(boolean immediateFlush){
289294
this.immediateFlush = immediateFlush;
290295
return this;
291296
}
297+
298+
/**
299+
* Gets html meta info. This meta info will be passed with to {@link com.itextpdf.kernel.counter.EventCounter}
300+
* with {@link com.itextpdf.html2pdf.events.PdfHtmlEvent} and can be used to determine event origin.
301+
*
302+
* @return converter's {@link IMetaInfo}
303+
*/
304+
public IMetaInfo getEventCountingMetaInfo() {
305+
return metaInfo;
306+
}
307+
308+
/**
309+
* Sets html meta info. This meta info will be passed with to {@link com.itextpdf.kernel.counter.EventCounter}
310+
* with {@link com.itextpdf.html2pdf.events.PdfHtmlEvent} and can be used to determine event origin.
311+
*
312+
* @param metaInfo meta info to set
313+
* @return this {@link ConverterProperties} instance
314+
*/
315+
public ConverterProperties setEventCountingMetaInfo(IMetaInfo metaInfo) {
316+
this.metaInfo = metaInfo;
317+
return this;
318+
}
292319
}

src/main/java/com/itextpdf/html2pdf/HtmlConverter.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ This file is part of the iText (R) project.
4949
import com.itextpdf.html2pdf.html.impl.jsoup.JsoupHtmlParser;
5050
import com.itextpdf.html2pdf.html.node.IDocumentNode;
5151
import com.itextpdf.io.util.FileUtil;
52+
import com.itextpdf.kernel.counter.event.IMetaInfo;
53+
import com.itextpdf.kernel.pdf.DocumentProperties;
5254
import com.itextpdf.kernel.pdf.PdfDocument;
5355
import com.itextpdf.kernel.pdf.PdfWriter;
5456
import com.itextpdf.layout.Document;
@@ -132,7 +134,7 @@ public static void convertToPdf(String html, PdfWriter pdfWriter) throws IOExcep
132134
* @throws IOException Signals that an I/O exception has occurred.
133135
*/
134136
public static void convertToPdf(String html, PdfWriter pdfWriter, ConverterProperties converterProperties) throws IOException {
135-
convertToPdf(html, new PdfDocument(pdfWriter), converterProperties);
137+
convertToPdf(html, new PdfDocument(pdfWriter, new DocumentProperties().setEventCountingMetaInfo(new HtmlMetaInfo())), converterProperties);
136138
}
137139

138140
/**
@@ -227,7 +229,7 @@ public static void convertToPdf(InputStream htmlStream, PdfDocument pdfDocument)
227229
* @throws IOException Signals that an I/O exception has occurred.
228230
*/
229231
public static void convertToPdf(InputStream htmlStream, PdfWriter pdfWriter) throws IOException {
230-
convertToPdf(htmlStream, new PdfDocument(pdfWriter));
232+
convertToPdf(htmlStream, new PdfDocument(pdfWriter, new DocumentProperties().setEventCountingMetaInfo(new HtmlMetaInfo())));
231233
}
232234

233235
/**
@@ -241,7 +243,7 @@ public static void convertToPdf(InputStream htmlStream, PdfWriter pdfWriter) thr
241243
* @throws IOException Signals that an I/O exception has occurred.
242244
*/
243245
public static void convertToPdf(InputStream htmlStream, PdfWriter pdfWriter, ConverterProperties converterProperties) throws IOException {
244-
convertToPdf(htmlStream, new PdfDocument(pdfWriter), converterProperties);
246+
convertToPdf(htmlStream, new PdfDocument(pdfWriter, new DocumentProperties().setEventCountingMetaInfo(new HtmlMetaInfo())), converterProperties);
245247
}
246248

247249
/**
@@ -553,4 +555,9 @@ public static List<IElement> convertToElements(InputStream htmlStream, Converter
553555
IDocumentNode doc = parser.parse(htmlStream, converterProperties != null ? converterProperties.getCharset() : null);
554556
return Attacher.attach(doc, converterProperties);
555557
}
558+
559+
private static class HtmlMetaInfo implements IMetaInfo {
560+
561+
private static final long serialVersionUID = -295587336698550627L;
562+
}
556563
}

src/main/java/com/itextpdf/html2pdf/attach/ProcessorContext.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ This file is part of the iText (R) project.
5555
import com.itextpdf.html2pdf.resolver.form.RadioCheckResolver;
5656
import com.itextpdf.html2pdf.resolver.resource.ResourceResolver;
5757
import com.itextpdf.io.font.FontProgram;
58+
import com.itextpdf.kernel.counter.event.IMetaInfo;
5859
import com.itextpdf.kernel.pdf.PdfDocument;
5960
import com.itextpdf.layout.font.FontInfo;
6061
import com.itextpdf.layout.font.FontProvider;
@@ -115,6 +116,9 @@ public class ProcessorContext {
115116
/** The PDF document. */
116117
private PdfDocument pdfDocument;
117118

119+
/** The Processor meta info */
120+
private IMetaInfo metaInfo;
121+
118122
/**
119123
* Instantiates a new {@link ProcessorContext} instance.
120124
*
@@ -165,6 +169,7 @@ public ProcessorContext(ConverterProperties converterProperties) {
165169
formFieldNameResolver = new FormFieldNameResolver();
166170
radioCheckResolver = new RadioCheckResolver();
167171
immediateFlush = converterProperties.isImmediateFlush();
172+
metaInfo = converterProperties.getEventCountingMetaInfo();
168173
}
169174

170175
/**
@@ -381,4 +386,14 @@ public String getBaseUri(){
381386
public boolean isImmediateFlush(){
382387
return immediateFlush;
383388
}
389+
390+
/**
391+
* Gets html meta info. This meta info will be passed with to {@link com.itextpdf.kernel.counter.EventCounter}
392+
* with {@link com.itextpdf.html2pdf.events.PdfHtmlEvent} and can be used to determine event origin.
393+
*
394+
* @return html meta info
395+
*/
396+
public IMetaInfo getEventCountingMetaInfo() {
397+
return metaInfo;
398+
}
384399
}

src/main/java/com/itextpdf/html2pdf/attach/impl/DefaultHtmlProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public List<com.itextpdf.layout.element.IElement> processElements(INode root) {
229229
}
230230
cssResolver = null;
231231
roots = null;
232-
EventCounterHandler.getInstance().onEvent(PdfHtmlEvent.CONVERT, getClass());
232+
EventCounterHandler.getInstance().onEvent(PdfHtmlEvent.CONVERT, context.getEventCountingMetaInfo(), getClass());
233233
return elements;
234234
}
235235

@@ -293,7 +293,7 @@ public Document processDocument(INode root, PdfDocument pdfDocument) {
293293
}
294294
cssResolver = null;
295295
roots = null;
296-
EventCounterHandler.getInstance().onEvent(PdfHtmlEvent.CONVERT, getClass());
296+
EventCounterHandler.getInstance().onEvent(PdfHtmlEvent.CONVERT, context.getEventCountingMetaInfo(), getClass());
297297
return doc;
298298
}
299299

0 commit comments

Comments
 (0)