Skip to content

Commit 46009b1

Browse files
committed
[RELEASE] iText 7 pdfHTML - 1.0.4
https://github.com/itext/i7j-pdfhtml/releases/tag/1.0.4 * release/1.0.4: [RELEASE] 1.0.4-SNAPSHOT -> 1.0.4 Add new uri resolver tests. Fix processing base url as file url. Major refactoring. Add new tests. Update cmp files [RELEASE] 1.0.3-SNAPSHOT -> 1.0.4-SNAPSHOT Signed-off-by: iText Software (Community) <[email protected]>
2 parents 91f030a + 9368f74 commit 46009b1

File tree

47 files changed

+469
-37
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+469
-37
lines changed

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<parent>
66
<groupId>com.itextpdf</groupId>
77
<artifactId>root</artifactId>
8-
<version>7.0.6</version>
8+
<version>7.0.7</version>
99
<relativePath />
1010
</parent>
1111

1212
<artifactId>html2pdf</artifactId>
13-
<version>1.0.3</version>
13+
<version>1.0.4</version>
1414

1515
<name>pdfHTML</name>
1616
<description>pdfHTML is an iText 7 add-on that lets you to parse (X)HTML snippets and the associated CSS and converts

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ This file is part of the iText (R) project.
6666
import java.io.IOException;
6767
import java.io.InputStream;
6868
import java.io.OutputStream;
69+
import java.nio.file.Paths;
6970
import java.util.List;
7071

7172
/**
@@ -172,9 +173,9 @@ public static void convertToPdf(File htmlFile, File pdfFile) throws IOException
172173
*/
173174
public static void convertToPdf(File htmlFile, File pdfFile, ConverterProperties converterProperties) throws IOException {
174175
if (converterProperties == null) {
175-
converterProperties = new ConverterProperties().setBaseUri(FileUtil.getParentDirectory(htmlFile.getAbsolutePath()) + File.separator);
176+
converterProperties = new ConverterProperties().setBaseUri(Paths.get(FileUtil.getParentDirectory(htmlFile.getAbsolutePath())).toUri().toURL().toExternalForm() + File.separator);
176177
} else if (converterProperties.getBaseUri() == null) {
177-
converterProperties = new ConverterProperties(converterProperties).setBaseUri(FileUtil.getParentDirectory(htmlFile.getAbsolutePath()) + File.separator);
178+
converterProperties = new ConverterProperties(converterProperties).setBaseUri(Paths.get(FileUtil.getParentDirectory(htmlFile.getAbsolutePath())).toUri().toURL().toExternalForm() + File.separator);
178179
}
179180
try (FileInputStream fileInputStream = new FileInputStream(htmlFile.getAbsolutePath());
180181
FileOutputStream fileOutputStream = new FileOutputStream(pdfFile.getAbsolutePath())) {

src/main/java/com/itextpdf/html2pdf/resolver/resource/UriResolver.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ This file is part of the iText (R) project.
4848
import java.net.URI;
4949
import java.net.URISyntaxException;
5050
import java.net.URL;
51+
import java.nio.file.Files;
5152
import java.nio.file.Path;
5253
import java.nio.file.Paths;
5354
import java.util.regex.Matcher;
@@ -93,7 +94,6 @@ public URL resolveAgainstBaseUri(String uriString) throws MalformedURLException
9394
URL resolvedUrl = null;
9495
uriString = uriString.trim();
9596
// decode and then encode uri string in order to process unsafe characters correctly
96-
String scheme = getUriStringScheme(uriString);
9797
uriString = UriEncodeUtil.encode(uriString);
9898
if (isLocalBaseUri) {
9999
// remove leading slashes in order to always concatenate such resource URIs: we don't want to scatter all
@@ -119,16 +119,23 @@ public URL resolveAgainstBaseUri(String uriString) throws MalformedURLException
119119
return resolvedUrl;
120120
}
121121

122+
/**
123+
* Check if baseURI is local
124+
*
125+
* @return true if baseURI is local, otherwise false
126+
*/
127+
public boolean isLocalBaseUri() {
128+
return isLocalBaseUri;
129+
}
130+
122131
/**
123132
* Resolves the base URI to an URL or path.
124133
*
125134
* @param base the base URI
126135
*/
127136
private void resolveBaseUrlOrPath(String base) {
128137
base = base.trim();
129-
String scheme = getUriStringScheme(base);
130-
base = UriEncodeUtil.encode(base);
131-
baseUrl = baseUriAsUrl(base);
138+
baseUrl = baseUriAsUrl(UriEncodeUtil.encode(base));
132139
if (baseUrl == null) {
133140
baseUrl = uriAsFileUrl(base);
134141
}
@@ -171,7 +178,14 @@ private URL uriAsFileUrl(String baseUriString) {
171178
URL baseAsFileUrl = null;
172179
try {
173180
Path path = Paths.get(baseUriString);
174-
baseAsFileUrl = path.toAbsolutePath().normalize().toUri().toURL();
181+
if (isPathRooted(path, baseUriString)) {
182+
String str = "file:///" + encode(path, path.toAbsolutePath().normalize().toString());
183+
baseAsFileUrl = new URI(str).toURL();
184+
} else {
185+
String str = encode(path, baseUriString);
186+
URL base = Paths.get("").toUri().toURL();
187+
baseAsFileUrl = new URL(base, str);
188+
}
175189
isLocalBaseUri = true;
176190
} catch (Exception ignored) {
177191
}
@@ -199,12 +213,17 @@ private String getUriStringScheme(String uriString) {
199213
return result;
200214
}
201215

202-
/**
203-
* Check if baseURI is local
204-
*
205-
* @return true if baseURI is local, otherwise false
206-
*/
207-
public boolean isLocalBaseUri() {
208-
return isLocalBaseUri;
216+
private String encode(Path path, String str) {
217+
str = str.replace("\\", "/");
218+
str = UriEncodeUtil.encode(str);
219+
if (Files.isDirectory(path) && !str.endsWith("/")) {
220+
str += "/";
221+
}
222+
str = str.replaceFirst("/*\\\\*", "");
223+
return str;
224+
}
225+
226+
private boolean isPathRooted(Path path, String str) {
227+
return path.isAbsolute() || str.startsWith("/");
209228
}
210229
}

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

Lines changed: 164 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,38 @@ public void resourceResolverTest07() throws IOException, InterruptedException {
8989
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff07_"));
9090
}
9191

92+
@Test
93+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
94+
public void resourceResolverTest07A() throws IOException, InterruptedException {
95+
String baseUri = sourceFolder + "%23r%e%2525s@o%25urces/";
96+
97+
String outPdf = destinationFolder + "resourceResolverTest07A.pdf";
98+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest07A.pdf";
99+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest07A.html");
100+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
101+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
102+
}
103+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff07A_"));
104+
}
105+
106+
@Test
107+
public void resourceResolverTest07B() throws IOException, InterruptedException {
108+
String outPdf = destinationFolder + "resourceResolverTest07B.pdf";
109+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest07B.pdf";
110+
HtmlConverter.convertToPdf(new File(sourceFolder + "#r%e%25s@o%urces/resourceResolverTest07B.html"), new File(outPdf));
111+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff07B_"));
112+
}
113+
114+
@Test
115+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
116+
public void resourceResolverTest07C() throws IOException, InterruptedException {
117+
String outPdf = destinationFolder + "resourceResolverTest07C.pdf";
118+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest07C.pdf";
119+
HtmlConverter.convertToPdf(new File(sourceFolder + "#r%e%25s@o%urces/resourceResolverTest07C.html"), new File(outPdf),
120+
new ConverterProperties().setBaseUri(sourceFolder + "#r%e%25s@o%urces/.."));
121+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff07C_"));
122+
}
123+
92124
@Test
93125
public void resourceResolverTest09() throws IOException, InterruptedException {
94126
String outPdf = destinationFolder + "resourceResolverTest09.pdf";
@@ -120,44 +152,101 @@ public void resourceResolverTest11() throws IOException, InterruptedException {
120152
}
121153

122154
@Test
123-
@Ignore("DEVSIX-1668")
124-
public void resourceResolverTest12() throws IOException, InterruptedException {
125-
String baseUri = sourceFolder + "path with spaces";
155+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
156+
public void resourceResolverTest12A() throws IOException, InterruptedException {
157+
String baseUri = sourceFolder + "path%with%spaces/";
126158

127-
String outPdf = destinationFolder + "resourceResolverTest12.pdf";
128-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12.pdf";
129-
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12.html");
159+
String outPdf = destinationFolder + "resourceResolverTest12A.pdf";
160+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12A.pdf";
161+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12A.html");
130162
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
131163
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
132164
}
133-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12_"));
165+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12A_"));
134166
}
135167

136168
@Test
137-
public void resourceResolverTest13() throws IOException, InterruptedException {
138-
String baseUri = sourceFolder;
169+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
170+
public void resourceResolverTest12B() throws IOException, InterruptedException {
171+
String baseUri = sourceFolder + "path%25with%25spaces/";
139172

140-
String outPdf = destinationFolder + "resourceResolverTest13.pdf";
141-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest13.pdf";
142-
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest13.html");
173+
String outPdf = destinationFolder + "resourceResolverTest12B.pdf";
174+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12B.pdf";
175+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12B.html");
143176
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
144177
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
145178
}
146-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff13_"));
179+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12B_"));
180+
}
181+
182+
@Test
183+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
184+
public void resourceResolverTest12C() throws IOException, InterruptedException {
185+
String baseUri = sourceFolder + "path%2525with%2525spaces/";
186+
187+
String outPdf = destinationFolder + "resourceResolverTest12C.pdf";
188+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12C.pdf";
189+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12C.html");
190+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
191+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
192+
}
193+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12C_"));
194+
}
195+
196+
197+
@Test
198+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
199+
public void resourceResolverTest12D() throws IOException, InterruptedException {
200+
String baseUri = sourceFolder + "path with spaces/";
201+
202+
String outPdf = destinationFolder + "resourceResolverTest12D.pdf";
203+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12D.pdf";
204+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12D.html");
205+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
206+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
207+
}
208+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12D_"));
209+
}
210+
211+
@Test
212+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
213+
public void resourceResolverTest12E() throws IOException, InterruptedException {
214+
String baseUri = sourceFolder + "path%20with%20spaces/";
215+
216+
String outPdf = destinationFolder + "resourceResolverTest12E.pdf";
217+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12E.pdf";
218+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12E.html");
219+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
220+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
221+
}
222+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12E_"));
147223
}
148224

149225
@Test
150-
@Ignore("DEVSIX-1668")
151-
public void resourceResolverTest14() throws IOException, InterruptedException {
152-
String baseUri = sourceFolder + "path%20with%20spaces";
226+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
227+
public void resourceResolverTest12F() throws IOException, InterruptedException {
228+
String baseUri = sourceFolder + "path%2520with%2520spaces/";
153229

154-
String outPdf = destinationFolder + "resourceResolverTest14.pdf";
155-
String cmpPdf = sourceFolder + "cmp_resourceResolverTest14.pdf";
156-
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12.html");
230+
String outPdf = destinationFolder + "resourceResolverTest12F.pdf";
231+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest12F.pdf";
232+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest12F.html");
157233
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
158234
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
159235
}
160-
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff14_"));
236+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff12F_"));
237+
}
238+
239+
@Test
240+
public void resourceResolverTest13() throws IOException, InterruptedException {
241+
String baseUri = sourceFolder;
242+
243+
String outPdf = destinationFolder + "resourceResolverTest13.pdf";
244+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest13.pdf";
245+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest13.html");
246+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
247+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
248+
}
249+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff13_"));
161250
}
162251

163252
@Test
@@ -173,6 +262,61 @@ public void resourceResolverTest15() throws IOException, InterruptedException {
173262
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff15_"));
174263
}
175264

265+
@Test
266+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
267+
public void resourceResolverTest16A() throws IOException, InterruptedException {
268+
String baseUri = sourceFolder + "path/with/spaces/";
269+
270+
String outPdf = destinationFolder + "resourceResolverTest16A.pdf";
271+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest16A.pdf";
272+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest16A.html");
273+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
274+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
275+
}
276+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff16A_"));
277+
}
278+
279+
@Test
280+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
281+
public void resourceResolverTest16B() throws IOException, InterruptedException {
282+
String baseUri = sourceFolder + "path%2Fwith%2Fspaces/";
283+
284+
String outPdf = destinationFolder + "resourceResolverTest16B.pdf";
285+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest16B.pdf";
286+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest16B.html");
287+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
288+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
289+
}
290+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff16B_"));
291+
}
292+
293+
@Test
294+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
295+
public void resourceResolverTest16C() throws IOException, InterruptedException {
296+
String baseUri = sourceFolder + "path%252Fwith%252Fspaces/";
297+
298+
String outPdf = destinationFolder + "resourceResolverTest16C.pdf";
299+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest16C.pdf";
300+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest16C.html");
301+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
302+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
303+
}
304+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff16C_"));
305+
}
306+
307+
@Test
308+
@LogMessages(messages = @LogMessage(messageTemplate = LogMessageConstant.NO_WORKER_FOUND_FOR_TAG, count = 1))
309+
public void resourceResolverTest16D() throws IOException, InterruptedException {
310+
String baseUri = sourceFolder + "path%25252Fwith%25252Fspaces/";
311+
312+
String outPdf = destinationFolder + "resourceResolverTest16D.pdf";
313+
String cmpPdf = sourceFolder + "cmp_resourceResolverTest16D.pdf";
314+
try (FileInputStream fileInputStream = new FileInputStream(sourceFolder + "resourceResolverTest16D.html");
315+
FileOutputStream fileOutputStream = new FileOutputStream(outPdf)) {
316+
HtmlConverter.convertToPdf(fileInputStream, fileOutputStream, new ConverterProperties().setBaseUri(baseUri));
317+
}
318+
Assert.assertNull(new CompareTool().compareByContent(outPdf, cmpPdf, destinationFolder, "diff16D_"));
319+
}
176320
// TODO test with absolute http links for resources?
177321
// TODO test with http base URI?
178322
}

0 commit comments

Comments
 (0)