Skip to content

Commit 53b816e

Browse files
committed
Free resources after reading them
Add new test that makes sure fonts imported with @fontface are released after the conversion DEVSIX-2314
1 parent a12c53d commit 53b816e

File tree

6 files changed

+202
-26
lines changed

6 files changed

+202
-26
lines changed

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/ImgTagWorker.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,12 @@ This file is part of the iText (R) project.
5858
import com.itextpdf.svg.exceptions.SvgProcessingException;
5959
import com.itextpdf.svg.processors.ISvgProcessorResult;
6060
import com.itextpdf.svg.processors.impl.SvgConverterProperties;
61+
import org.slf4j.Logger;
62+
import org.slf4j.LoggerFactory;
6163

6264
import java.io.IOException;
6365
import java.io.InputStream;
6466

65-
import org.slf4j.Logger;
66-
import org.slf4j.LoggerFactory;
67-
6867
/**
6968
* TagWorker class for the {@code img} element.
7069
*/
@@ -100,14 +99,15 @@ public ImgTagWorker(IElementNode element, ProcessorContext context) {
10099
} else {
101100
byte[] resourceBytes = context.getResourceResolver().retrieveBytesFromResource(src);
102101
if (resourceBytes != null) {
103-
InputStream resourceStream = context.getResourceResolver().retrieveResourceAsInputStream(src);
104-
//Try with svg
105-
try {
106-
processAsSvg(resourceStream, context);
107-
} catch (SvgProcessingException spe) {
108-
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_PROCESS_IMAGE_AS_SVG, context.getBaseUri(), src));
109-
} catch(IOException ioe){
110-
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI,context.getBaseUri(),src));
102+
try (InputStream resourceStream = context.getResourceResolver().retrieveResourceAsInputStream(src)) {
103+
//Try with svg
104+
try {
105+
processAsSvg(resourceStream, context);
106+
} catch (SvgProcessingException spe) {
107+
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_PROCESS_IMAGE_AS_SVG, context.getBaseUri(), src));
108+
}
109+
} catch (IOException ioe) {
110+
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, context.getBaseUri(), src));
111111
}
112112
}
113113
}

src/main/java/com/itextpdf/html2pdf/attach/impl/tags/ObjectTagWorker.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,12 @@ This file is part of the iText (R) project.
5656
import com.itextpdf.svg.exceptions.SvgProcessingException;
5757
import com.itextpdf.svg.processors.ISvgProcessorResult;
5858
import com.itextpdf.svg.processors.impl.SvgConverterProperties;
59+
import org.slf4j.Logger;
60+
import org.slf4j.LoggerFactory;
5961

6062
import java.io.IOException;
6163
import java.io.InputStream;
6264

63-
import org.slf4j.Logger;
64-
import org.slf4j.LoggerFactory;
65-
6665
/**
6766
* TagWorker class for the {@code object} element.
6867
*/
@@ -96,22 +95,24 @@ public ObjectTagWorker(IElementNode element, ProcessorContext context) {
9695
String type = element.getAttribute(AttributeConstants.TYPE);
9796
if (isSvgImage(type)) {
9897
//Use resource resolver to retrieve the URL
99-
InputStream svgStream = context.getResourceResolver().retrieveResourceAsInputStream(element.getAttribute(AttributeConstants.DATA));
100-
if(svgStream != null) {
101-
try {
102-
SvgConverterProperties svgConverterProperties = new SvgConverterProperties();
103-
svgConverterProperties.setBaseUri(context.getBaseUri())
104-
.setFontProvider(context.getFontProvider())
105-
.setMediaDeviceDescription(context.getDeviceDescription());
106-
res = SvgConverter.parseAndProcess(svgStream, svgConverterProperties);
107-
} catch (SvgProcessingException spe) {
108-
LOGGER.error(spe.getMessage());
109-
} catch(IOException ie){
110-
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI,context.getBaseUri(),element.getAttribute(AttributeConstants.DATA)));
98+
try (InputStream svgStream = context.getResourceResolver().retrieveResourceAsInputStream(element.getAttribute(AttributeConstants.DATA))) {
99+
if (svgStream != null) {
100+
try {
101+
SvgConverterProperties svgConverterProperties = new SvgConverterProperties();
102+
svgConverterProperties.setBaseUri(context.getBaseUri())
103+
.setFontProvider(context.getFontProvider())
104+
.setMediaDeviceDescription(context.getDeviceDescription());
105+
res = SvgConverter.parseAndProcess(svgStream, svgConverterProperties);
106+
} catch (SvgProcessingException spe) {
107+
LOGGER.error(spe.getMessage());
108+
}
111109
}
110+
} catch (IOException ie) {
111+
LOGGER.error(MessageFormatUtil.format(LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI, context.getBaseUri(), element.getAttribute(AttributeConstants.DATA)));
112112
}
113113
}
114114
}
115+
115116
@Override
116117
public void processEnd(IElementNode element, ProcessorContext context) {
117118
if (context.getPdfDocument() != null) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.itextpdf.html2pdf.resolver.font;
2+
3+
import com.itextpdf.html2pdf.HtmlConverter;
4+
import com.itextpdf.test.ExtendedITextTest;
5+
import com.itextpdf.test.annotations.type.IntegrationTest;
6+
import org.junit.Assert;
7+
import org.junit.BeforeClass;
8+
import org.junit.Test;
9+
import org.junit.experimental.categories.Category;
10+
11+
import java.io.File;
12+
import java.io.IOException;
13+
import java.nio.file.Files;
14+
import java.nio.file.Paths;
15+
16+
@Category(IntegrationTest.class)
17+
public class LocalFontResolverReleaseTest extends ExtendedITextTest {
18+
19+
public static final String sourceFolder = "./src/test/resources/com/itextpdf/html2pdf/resolver/font/LocalFontResolverReleaseTest/";
20+
public static final String destinationFolder = "./target/test/com/itextpdf/html2pdf/resolver/font/LocalFontResolverReleaseTest/";
21+
22+
@BeforeClass
23+
public static void beforeClass() {
24+
createOrClearDestinationFolder(destinationFolder);
25+
}
26+
27+
@Test
28+
public void testThatLocalFontIsReleasedAfterConversion() throws IOException {
29+
String htmlFileName = "test.html";
30+
String fontFileName = "NotoSans-Regular.ttf";
31+
String sourceHtmlFile = sourceFolder + htmlFileName;
32+
String sourceFontFile = sourceFolder + fontFileName;
33+
34+
String workDir = destinationFolder + "work/";
35+
createDestinationFolder(workDir);
36+
String targetPdfFile = workDir + "target.pdf";
37+
38+
String workDirHtmlFile = workDir + htmlFileName;
39+
String workDirFontFile = workDir + fontFileName;
40+
41+
Files.copy(Paths.get(sourceHtmlFile), Paths.get(workDirHtmlFile));
42+
Files.copy(Paths.get(sourceFontFile), Paths.get(workDirFontFile));
43+
44+
// The issue cannot be reproduced consistently and automatically with just single conversion for some reason
45+
// Probably related to some optimizations done by JVM
46+
// It already reproduces with 2 conversions, but here we do more to get rid of possible JVM even trickier optimizations
47+
for (int i = 0; i < 5; i++) {
48+
HtmlConverter.convertToPdf(new File(workDirHtmlFile), new File(targetPdfFile));
49+
}
50+
51+
// The resource must be freed after the conversion
52+
File resourceToBeremoved = new File(workDirFontFile);
53+
resourceToBeremoved.delete();
54+
Assert.assertFalse(resourceToBeremoved.exists());
55+
}
56+
57+
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
This Font Software is licensed under the SIL Open Font License,
2+
Version 1.1.
3+
4+
This license is copied below, and is also available with a FAQ at:
5+
http://scripts.sil.org/OFL
6+
7+
-----------------------------------------------------------
8+
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
9+
-----------------------------------------------------------
10+
11+
PREAMBLE
12+
The goals of the Open Font License (OFL) are to stimulate worldwide
13+
development of collaborative font projects, to support the font
14+
creation efforts of academic and linguistic communities, and to
15+
provide a free and open framework in which fonts may be shared and
16+
improved in partnership with others.
17+
18+
The OFL allows the licensed fonts to be used, studied, modified and
19+
redistributed freely as long as they are not sold by themselves. The
20+
fonts, including any derivative works, can be bundled, embedded,
21+
redistributed and/or sold with any software provided that any reserved
22+
names are not used by derivative works. The fonts and derivatives,
23+
however, cannot be released under any other type of license. The
24+
requirement for fonts to remain under this license does not apply to
25+
any document created using the fonts or their derivatives.
26+
27+
DEFINITIONS
28+
"Font Software" refers to the set of files released by the Copyright
29+
Holder(s) under this license and clearly marked as such. This may
30+
include source files, build scripts and documentation.
31+
32+
"Reserved Font Name" refers to any names specified as such after the
33+
copyright statement(s).
34+
35+
"Original Version" refers to the collection of Font Software
36+
components as distributed by the Copyright Holder(s).
37+
38+
"Modified Version" refers to any derivative made by adding to,
39+
deleting, or substituting -- in part or in whole -- any of the
40+
components of the Original Version, by changing formats or by porting
41+
the Font Software to a new environment.
42+
43+
"Author" refers to any designer, engineer, programmer, technical
44+
writer or other person who contributed to the Font Software.
45+
46+
PERMISSION & CONDITIONS
47+
Permission is hereby granted, free of charge, to any person obtaining
48+
a copy of the Font Software, to use, study, copy, merge, embed,
49+
modify, redistribute, and sell modified and unmodified copies of the
50+
Font Software, subject to the following conditions:
51+
52+
1) Neither the Font Software nor any of its individual components, in
53+
Original or Modified Versions, may be sold by itself.
54+
55+
2) Original or Modified Versions of the Font Software may be bundled,
56+
redistributed and/or sold with any software, provided that each copy
57+
contains the above copyright notice and this license. These can be
58+
included either as stand-alone text files, human-readable headers or
59+
in the appropriate machine-readable metadata fields within text or
60+
binary files as long as those fields can be easily viewed by the user.
61+
62+
3) No Modified Version of the Font Software may use the Reserved Font
63+
Name(s) unless explicit written permission is granted by the
64+
corresponding Copyright Holder. This restriction only applies to the
65+
primary font name as presented to the users.
66+
67+
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
68+
Software shall not be used to promote, endorse or advertise any
69+
Modified Version, except to acknowledge the contribution(s) of the
70+
Copyright Holder(s) and the Author(s) or with their explicit written
71+
permission.
72+
73+
5) The Font Software, modified or unmodified, in part or in whole,
74+
must be distributed entirely under this license, and must not be
75+
distributed under any other license. The requirement for fonts to
76+
remain under this license does not apply to any document created using
77+
the Font Software.
78+
79+
TERMINATION
80+
This license becomes null and void if any of the above conditions are
81+
not met.
82+
83+
DISCLAIMER
84+
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
85+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
86+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
87+
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
88+
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
89+
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
90+
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
91+
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
92+
OTHER DEALINGS IN THE FONT SOFTWARE.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!doctype html><html><head><style>html, body {
2+
margin: 0;
3+
padding: 0;
4+
width: 100%;
5+
height: 100%;
6+
}
7+
8+
*[data-dito-element=checkbox] {
9+
display: flex;
10+
}</style>
11+
<meta data-dito-template-type="output">
12+
<meta charset="UTF-8">
13+
<style>@font-face {
14+
font-family: "custom";
15+
src: url("NotoSans-Regular.ttf")
16+
}</style><style>
17+
span[data-dito-page-counter-current]::before {
18+
content: counter(page);
19+
}
20+
21+
span[data-dito-page-counter-total]::before {
22+
content: counter(pages);
23+
}
24+
</style></head>
25+
26+
<body data-dito-element="subform" style="font-family:custom"><div data-dito-element="rich-text"><div>Text</div></div><div data-dito-element="rich-text"><div>Text</div></div><div data-dito-element="rich-text"><div>Text</div></div></body></html>

0 commit comments

Comments
 (0)