Skip to content

Commit f40bcfe

Browse files
SnipxiText-CI
authored andcommitted
Free resources after reading them
Add new test that makes sure fonts imported with @fontface are released after the conversion DEVSIX-2314 Autoported commit. Original commit hash: [53b816e0]
1 parent 989ded7 commit f40bcfe

File tree

7 files changed

+192
-23
lines changed

7 files changed

+192
-23
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
using System;
2+
using System.IO;
3+
using iText.Html2pdf;
4+
using iText.Test;
5+
6+
namespace iText.Html2pdf.Resolver.Font {
7+
public class LocalFontResolverReleaseTest : ExtendedITextTest {
8+
public static readonly String sourceFolder = iText.Test.TestUtil.GetParentProjectDirectory(NUnit.Framework.TestContext
9+
.CurrentContext.TestDirectory) + "/resources/itext/html2pdf/resolver/font/LocalFontResolverReleaseTest/";
10+
11+
public static readonly String destinationFolder = NUnit.Framework.TestContext.CurrentContext.TestDirectory
12+
+ "/test/itext/html2pdf/resolver/font/LocalFontResolverReleaseTest/";
13+
14+
[NUnit.Framework.OneTimeSetUp]
15+
public static void BeforeClass() {
16+
CreateOrClearDestinationFolder(destinationFolder);
17+
}
18+
19+
/// <exception cref="System.IO.IOException"/>
20+
[NUnit.Framework.Test]
21+
public virtual void TestThatLocalFontIsReleasedAfterConversion() {
22+
String htmlFileName = "test.html";
23+
String fontFileName = "NotoSans-Regular.ttf";
24+
String sourceHtmlFile = sourceFolder + htmlFileName;
25+
String sourceFontFile = sourceFolder + fontFileName;
26+
String workDir = destinationFolder + "work/";
27+
CreateDestinationFolder(workDir);
28+
String targetPdfFile = workDir + "target.pdf";
29+
String workDirHtmlFile = workDir + htmlFileName;
30+
String workDirFontFile = workDir + fontFileName;
31+
File.Copy(Path.Combine(sourceHtmlFile), Path.Combine(workDirHtmlFile));
32+
File.Copy(Path.Combine(sourceFontFile), Path.Combine(workDirFontFile));
33+
// The issue cannot be reproduced consistently and automatically with just single conversion for some reason
34+
// Probably related to some optimizations done by JVM
35+
// It already reproduces with 2 conversions, but here we do more to get rid of possible JVM even trickier optimizations
36+
for (int i = 0; i < 5; i++) {
37+
HtmlConverter.ConvertToPdf(new FileInfo(workDirHtmlFile), new FileInfo(targetPdfFile));
38+
}
39+
// The resource must be freed after the conversion
40+
FileInfo resourceToBeremoved = new FileInfo(workDirFontFile);
41+
resourceToBeremoved.Delete();
42+
NUnit.Framework.Assert.IsFalse(resourceToBeremoved.Exists);
43+
}
44+
}
45+
}
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>

itext/itext.html2pdf/itext/html2pdf/attach/impl/tags/ImgTagWorker.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,17 @@ public ImgTagWorker(IElementNode element, ProcessorContext context) {
9292
else {
9393
byte[] resourceBytes = context.GetResourceResolver().RetrieveBytesFromResource(src);
9494
if (resourceBytes != null) {
95-
Stream resourceStream = context.GetResourceResolver().RetrieveResourceAsInputStream(src);
96-
//Try with svg
9795
try {
98-
ProcessAsSvg(resourceStream, context);
99-
}
100-
catch (SvgProcessingException) {
101-
LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_PROCESS_IMAGE_AS_SVG, context
102-
.GetBaseUri(), src));
96+
using (Stream resourceStream = context.GetResourceResolver().RetrieveResourceAsInputStream(src)) {
97+
//Try with svg
98+
try {
99+
ProcessAsSvg(resourceStream, context);
100+
}
101+
catch (SvgProcessingException) {
102+
LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_PROCESS_IMAGE_AS_SVG, context
103+
.GetBaseUri(), src));
104+
}
105+
}
103106
}
104107
catch (System.IO.IOException) {
105108
LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI

itext/itext.html2pdf/itext/html2pdf/attach/impl/tags/ObjectTagWorker.cs

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,26 @@ public ObjectTagWorker(IElementNode element, ProcessorContext context) {
8989
String type = element.GetAttribute(AttributeConstants.TYPE);
9090
if (IsSvgImage(type)) {
9191
//Use resource resolver to retrieve the URL
92-
Stream svgStream = context.GetResourceResolver().RetrieveResourceAsInputStream(element.GetAttribute(AttributeConstants
93-
.DATA));
94-
if (svgStream != null) {
95-
try {
96-
SvgConverterProperties svgConverterProperties = new SvgConverterProperties();
97-
svgConverterProperties.SetBaseUri(context.GetBaseUri()).SetFontProvider(context.GetFontProvider()).SetMediaDeviceDescription
98-
(context.GetDeviceDescription());
99-
res = SvgConverter.ParseAndProcess(svgStream, svgConverterProperties);
100-
}
101-
catch (SvgProcessingException spe) {
102-
LOGGER.Error(spe.Message);
103-
}
104-
catch (System.IO.IOException) {
105-
LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI
106-
, context.GetBaseUri(), element.GetAttribute(AttributeConstants.DATA)));
92+
try {
93+
using (Stream svgStream = context.GetResourceResolver().RetrieveResourceAsInputStream(element.GetAttribute
94+
(AttributeConstants.DATA))) {
95+
if (svgStream != null) {
96+
try {
97+
SvgConverterProperties svgConverterProperties = new SvgConverterProperties();
98+
svgConverterProperties.SetBaseUri(context.GetBaseUri()).SetFontProvider(context.GetFontProvider()).SetMediaDeviceDescription
99+
(context.GetDeviceDescription());
100+
res = SvgConverter.ParseAndProcess(svgStream, svgConverterProperties);
101+
}
102+
catch (SvgProcessingException spe) {
103+
LOGGER.Error(spe.Message);
104+
}
105+
}
107106
}
108107
}
108+
catch (System.IO.IOException) {
109+
LOGGER.Error(MessageFormatUtil.Format(iText.Html2pdf.LogMessageConstant.UNABLE_TO_RETRIEVE_STREAM_WITH_GIVEN_BASE_URI
110+
, context.GetBaseUri(), element.GetAttribute(AttributeConstants.DATA)));
111+
}
109112
}
110113
}
111114

port-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
a12c53da342691a2c4d79afce8f4dc9c3fbcc4e1
1+
53b816e01ae0a2696e4b24f74b16b1d5b3490919

0 commit comments

Comments
 (0)