Skip to content

Commit 3013027

Browse files
committed
Use try-with-resources in HtmlConverter
1 parent 9c4e320 commit 3013027

File tree

6 files changed

+28
-68
lines changed

6 files changed

+28
-68
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,10 @@ public static void convertToPdf(File htmlFile, File pdfFile, ConverterProperties
164164
} else if (converterProperties.getBaseUri() == null) {
165165
converterProperties = new ConverterProperties(converterProperties).setBaseUri(FileUtil.getParentDirectory(htmlFile.getAbsolutePath()) + File.separator);
166166
}
167-
convertToPdf(new FileInputStream(htmlFile.getAbsolutePath()), new FileOutputStream(pdfFile.getAbsolutePath()), converterProperties);
167+
try (FileInputStream fileInputStream = new FileInputStream(htmlFile.getAbsolutePath());
168+
FileOutputStream fileOutputStream = new FileOutputStream(pdfFile.getAbsolutePath())) {
169+
convertToPdf(fileInputStream, fileOutputStream, converterProperties);
170+
}
168171
}
169172

170173
/**

src/test/java/com/itextpdf/html2pdf/ResourceResolverTest.java

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,64 +69,17 @@ public static void beforeClass() {
6969
createOrClearDestinationFolder(destinationFolder);
7070
}
7171

72-
@Test
73-
public void resourceResolverTest01() throws IOException, InterruptedException {
74-
String baseUri = sourceFolder + "resourceResolverTest01.html";
75-
String outPdf = destinationFolder + "resourceResolverTest01.pdf";
76-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest01.pdf";
77-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "resourceResolverTest01.html"), new FileOutputStream(outPdf), new ConverterProperties().setBaseUri(baseUri));
78-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff01_"));
79-
}
80-
81-
@Test
82-
@LogMessages(messages = {@LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_RETRIEVE_IMAGE_WITH_GIVEN_BASE_URI),
83-
@LogMessage(messageTemplate = LogMessageConstant.UNABLE_TO_PROCESS_EXTERNAL_CSS_FILE),
84-
@LogMessage(messageTemplate = LogMessageConstant.WORKER_UNABLE_TO_PROCESS_OTHER_WORKER)})
85-
public void resourceResolverTest02() throws IOException, InterruptedException {
86-
String baseUri = "/folderInDiskRoot";
87-
String outPdf = destinationFolder + "resourceResolverTest02.pdf";
88-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest02.pdf";
89-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "resourceResolverTest01.html"), new FileOutputStream(outPdf), new ConverterProperties().setBaseUri(baseUri));
90-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff02_"));
91-
}
92-
9372
@Test
9473
public void resourceResolverTest03() throws IOException, InterruptedException {
9574
String baseUri = sourceFolder + "res";
9675
String outPdf = destinationFolder + "resourceResolverTest03.pdf";
9776
String cmpPdf = sourceFolder + "cmp_resourceResolverTest03.pdf";
98-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "resourceResolverTest03.html"), new FileOutputStream(outPdf), new ConverterProperties().setBaseUri(baseUri));
77+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest03.html");
78+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
79+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
80+
}
9981
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff03_"));
10082
}
101-
102-
@Test
103-
public void resourceResolverTest04() throws IOException, InterruptedException {
104-
String invalidBaseUri = Paths.get("").toAbsolutePath().toString() + "/src/test/resources/com/itextpdf/html2pdf/ResourceResolverTest/" + "res";
105-
String outPdf = destinationFolder + "resourceResolverTest04.pdf";
106-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest04.pdf";
107-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "resourceResolverTest03.html"), new FileOutputStream(outPdf), new ConverterProperties().setBaseUri(invalidBaseUri));
108-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff04_"));
109-
}
110-
111-
@Test
112-
public void resourceResolverTest05() throws IOException, InterruptedException {
113-
// It is important to put a trailing slash in the end: if you specify base URI via absolute URI string,
114-
// you need to follow URI standards, in which a path without trailing slash is referring to a file.
115-
String baseUri = Paths.get("").toUri() + "src/test/resources/com/itextpdf/html2pdf/ResourceResolverTest/res/";
116-
String outPdf = destinationFolder + "resourceResolverTest05.pdf";
117-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest05.pdf";
118-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "resourceResolverTest03.html"), new FileOutputStream(outPdf), new ConverterProperties().setBaseUri(baseUri));
119-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff05_"));
120-
}
121-
122-
@Test
123-
public void resourceResolverTest06() throws IOException, InterruptedException {
124-
String outPdf = destinationFolder + "resourceResolverTest06.pdf";
125-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest06.pdf";
126-
HtmlConverter.convertToPdf(new File(sourceFolder + "resourceResolverTest01.html"), new File(outPdf));
127-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff06_"));
128-
}
129-
13083
@Test
13184
public void resourceResolverTest07() throws IOException, InterruptedException {
13285
String outPdf = destinationFolder + "resourceResolverTest07.pdf";

src/test/java/com/itextpdf/html2pdf/css/FloatTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,9 @@ public void responsiveIText() throws IOException, InterruptedException {
490490
mediaDescription.setWidth((float)pxWidth);
491491
converterProperties.setMediaDeviceDescription(mediaDescription);
492492
}
493-
HtmlConverter.convertToPdf(new FileInputStream(htmlSource), pdfDoc, converterProperties);
493+
try (FileInputStream fileInputStream = new FileInputStream(htmlSource)) {
494+
HtmlConverter.convertToPdf(fileInputStream, pdfDoc, converterProperties);
495+
}
494496
pdfDoc.close();
495497
}
496498

src/test/java/com/itextpdf/html2pdf/element/AbbrTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ public void abbrTest05() throws IOException, InterruptedException {
9696
private void runAbbrTest(String testName) throws IOException, InterruptedException {
9797
PdfDocument document = new PdfDocument(new PdfWriter(destinationFolder + testName + ".pdf"));
9898
document.setTagged();
99-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + testName + ".html"), document);
99+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + testName + ".html")) {
100+
HtmlConverter.convertToPdf(fileInputStream, document);
101+
}
100102
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + testName + ".pdf", sourceFolder + "cmp_" + testName + ".pdf", destinationFolder, "diff_" + testName));
101103
}
102104
}

src/test/java/com/itextpdf/html2pdf/element/LinkTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ public void linkTest01() throws IOException, InterruptedException {
7676
System.out.println("html: file:///" + UrlUtil.toNormalizedURI(sourceFolder + "linkTest01.html").getPath() + "\n");
7777
PdfDocument outDoc = new PdfDocument(new PdfWriter(destinationFolder + "linkTest01.pdf"));
7878
outDoc.setTagged();
79-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "linkTest01.html"), outDoc);
79+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "linkTest01.html")) {
80+
HtmlConverter.convertToPdf(fileInputStream, outDoc);
81+
}
8082
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "linkTest01.pdf", sourceFolder + "cmp_linkTest01.pdf", destinationFolder, "diff01_"));
8183
}
8284

@@ -115,23 +117,19 @@ public void linkTest06() throws IOException, InterruptedException {
115117
public void linkTest07() throws IOException, InterruptedException {
116118
PdfDocument outDoc = new PdfDocument(new PdfWriter(destinationFolder + "linkTest07.pdf"));
117119
outDoc.setTagged();
118-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "linkTest07.html"), outDoc);
120+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "linkTest07.html")) {
121+
HtmlConverter.convertToPdf(fileInputStream, outDoc);
122+
}
119123
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "linkTest07.pdf", sourceFolder + "cmp_linkTest07.pdf", destinationFolder, "diff07_"));
120124
}
121125

122-
@Test
123-
public void linkTest08() throws IOException, InterruptedException {
124-
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "linkTest08.pdf"));
125-
pdfDocument.setTagged();
126-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "linkTest08.html"), pdfDocument, new ConverterProperties().setBaseUri(sourceFolder));
127-
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "linkTest08.pdf", sourceFolder + "cmp_linkTest08.pdf", destinationFolder, "diff08_"));
128-
}
129-
130126
@Test
131127
public void linkTest09() throws IOException, InterruptedException {
132128
PdfDocument pdfDocument = new PdfDocument(new PdfWriter(destinationFolder + "linkTest09.pdf"));
133129
pdfDocument.setTagged();
134-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "linkTest09.html"), pdfDocument, new ConverterProperties().setBaseUri(sourceFolder));
130+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "linkTest09.html")) {
131+
HtmlConverter.convertToPdf(fileInputStream, pdfDocument, new ConverterProperties().setBaseUri(sourceFolder));
132+
}
135133
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "linkTest09.pdf", sourceFolder + "cmp_linkTest09.pdf", destinationFolder, "diff09_"));
136134
}
137135
}

src/test/java/com/itextpdf/html2pdf/element/ListTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ public void listTest17() throws IOException, InterruptedException {
170170
public void listToPdfaTest() throws IOException, InterruptedException {
171171
InputStream is = new FileInputStream(sourceFolder + "sRGB Color Space Profile.icm");
172172
PdfADocument pdfADocument = new PdfADocument(new PdfWriter(destinationFolder + "listToPdfa.pdf"), PdfAConformanceLevel.PDF_A_1B, new PdfOutputIntent("Custom", "", "http://www.color.org", "sRGB IEC61966-2.1", is));
173-
HtmlConverter.convertToPdf(new FileInputStream(sourceFolder + "listToPdfa.html"), pdfADocument, new ConverterProperties()
174-
.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT))
175-
.setFontProvider(new DefaultFontProvider(false, true, false)));
173+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "listToPdfa.html")) {
174+
HtmlConverter.convertToPdf(fileInputStream, pdfADocument, new ConverterProperties()
175+
.setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT))
176+
.setFontProvider(new DefaultFontProvider(false, true, false)));
177+
}
176178
Assert.assertNull(new CompareTool().compareByContent(destinationFolder + "listToPdfa.pdf", sourceFolder + "cmp_listToPdfa.pdf", destinationFolder, "diff99_"));
177179
}
178180

0 commit comments

Comments
 (0)