iText 7 - Parsing exception in PdfReader constructor closes the input stream unexpectedly - itext7

I'm facing an issue after upgrading to latest version of iText for .NET (7.1.15 -> 7.1.16).
Please see example code below:
Stream pdfStream = GetPdfStream(...);
try
{
IRandomAccessSource randomAccessSource = new RandomAccessSourceFactory().CreateSource(pdfStream);
ReaderProperties readerProperties = new ReaderProperties
{
// ...
};
using (var pdfReader = new PdfReader(randomAccessSource, readerProperties))
{
pdfReader.SetCloseStream(false);
// do things with pdfReader
}
}
catch
{
// ...
}
// reuse pdfStream somewhere else
In this situation if the file can not be parsed I'm getting the exception (expected):
iText.IO.IOException: PDF header not found.
at iText.Kernel.Pdf.PdfReader.GetOffsetTokeniser(IRandomAccessSource byteSource)
at iText.Kernel.Pdf.PdfReader..ctor(IRandomAccessSource byteSource, ReaderProperties properties)
But as an unexpected side effect it also closes the input Stream, which wasn't happening in the previous version (7.1.15). Because the exception happens in PdfReader constructor it also can not yet reach pdfReader.SetCloseStream.
Is there any way to prevent PdfReader from closing the externally provided input stream? Or any chance of a new constructor API that would take 'leaveOpen' parameter like .NET StreamReader does.

The bug is fixed in 7.2.0-SNAPSHOT and 7.1.17-SNAPSHOT versions.
You can fetch those versions from iText Artifactory using the following Maven configuration:
<repositories>
<repository>
<id>itext-snapshot</id>
<name>iText Repository - snapshots</name>
<url>https://repo.itextsupport.com/snapshot</url>
</repository>
<repositories>
Beware that 7.2.0-SNAPSHOT version contains breaking changes compared to 7.1.16-SNAPSHOT since 7.2.x line is considered a major upgrade compared to 7.1.x line.

Related

Spring WebClient hangs when posting file / inputstream / resource to Minio

I am developing a proxy service to a Minio server using WebClient that handles all Minio/S3 API endpoints. Most of them work fine, but I have encountered one case in which the PUT operation seems to get hung up when trying to set the body of the request to either an InputStream, a File, or a Resource pointing to it. (See epilogue at the bottom, as I'm left wondering where the problem really is.)
The only way I've found to make it work is to read the file contents to an in-memory byte array. The following baseline works, for example:
WebClient.UriSpec<WebClient.RequestBodySpec> uriSpec = client.method(request.getMethod());
WebClient.RequestBodySpec bodySpec = uriSpec.uri(uri);
WebClient.RequestHeadersSpec<?> headersSpec = bodySpec;
try {
// read file to byte array; works fine
byte[] bytes = Files.readAllBytes(Path.of(file.get().getFile().toURI()));
// set it to the request body
headersSpec = bodySpec.bodyValue(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
// manipulate some headers
headersSpec = headersSpec.headers(httpHeaders -> ...);
// execute the request; works fine in this scenario
return headersSpec.exchangeToMono(resp -> ...)
.doOnError(throwable -> log.error("Trouble proxying request: " + throwable.getMessage(), throwable));
However, every alternative that I try to stream this content instead, results in a request that seems to hang in the headersSpec.exchangeToMono invocation. I don't see any errors on the proxy service, and the client socket eventually gives up:
java.net.SocketTimeoutException: timeout
client-tester_1 | at okio.SocketAsyncTimeout.newTimeoutException(JvmOkio.kt:143) ~[okio-jvm-2.8.0.jar:na]
client-tester_1 | Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException:
Some examples of failure (or, paraphrasing Edison, I've successfully found at least a dozen ways that do not work):
// Use same byte array as above; Hangs
Resource resource = new ByteArrayResource(bytes);
headersSpec = bodySpec.bodyValue(resource);
// Read an input stream from the file (this one relies on a HttpMessageWriter<InputStream> that I configured on the client); Hangs
InputStream bodyStream = new BufferedInputStream(Files.newInputStream(Path.of(file.get().getFile().toURI())));
headersSpec = bodySpec.bodyValue(bodyStream);
// Resource for the file; Hangs
Resource resource = new FileSystemResource(Path.of(file.get().getFile().toURI()));
Flux<DataBuffer> flux = DataBufferUtils.read(resource, DefaultDataBufferFactory.sharedInstance, 4096);
headersSpec = bodySpec.body(flux, DataBuffer.class);
// Different resource; Hangs
Resource resource = new UrlResource(file.get().getFile().toURI());
headersSpec = bodySpec.bodyValue(resource);
// Try BodyInserters; Hangs
Flux<DataBuffer> flux = DataBufferUtils.read(Path.of(file.get().getFile().toURI()), DefaultDataBufferFactory.sharedInstance, 4096);
headersSpec = bodySpec.body(BodyInserters.fromDataBuffers(flux));
// Yet another attempt; Take a guess...
InputStream bodyStream = new BufferedInputStream(Files.newInputStream(Path.of(file.get().getFile().toURI())));
headersSpec = bodySpec.body(BodyInserters.fromResource(resource));
I'm using recent versions of the relevant libraries:
org.springframework.boot:spring-boot-starter-webflux -> 2.7.5
org.springframework.boot:spring-boot-starter-reactor-netty:2.7.5
org.springframework:spring-core:5.3.23
Epiloge I'm wondering if the problem is not necessarily with Spring/WebClient/Netty -- as many of these code samples were inspired by other examples I've found -- but rather by some nuance on the Minio server?

FlattenFields is not working as expected

We have been working on a POC with IText7 and getting an error when we try to FlattenFields. All we are trying to do is load a pdf template and inject values. The template which we are using was working fine with IText5.
Here is the exception message:
An exception of type 'iText.Kernel.PdfException' occurred in itext.kernel.dll but was not handled in user code
Additional information: unbalanced.begin.end.marked.content.operators
using (PdfDocument pdfDoc = new PdfDocument(new PdfReader(fileName), new PdfWriter(outputStream)))
{
PdfAcroForm stamper = PdfAcroForm.GetAcroForm(pdfDoc, true);
stamper.FlattenFields();
stamper.SetGenerateAppearance(true);
}
Regards
Shreenidhi B.R
Followed up with IText support folks and they said that this issue has been fixed in IText 7.0.1. I haven't had a chance to test it myself though.

How do I replace an existing Community file on IBM SmartCloud

I am trying to replace an existing Community file using the following java
Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("createVersion", "false");
fileEntry = fileService.updateCommunityFile(fis, fileUuid, fileName, communityLibraryId, paramsMap);
But it is returning a HTTP 411:Length required error.
I am using the latest build (1.1.5.20150520-1200.jar)
Does anyone have a suggestion as to what i am missing?
I tried recreating the issue but I am able to upload New version of Community file correctly with and without version, using the updateCommunityFile API. I do not get any Length related error. This is the snippet I am using :
java.io.File file = new java.io.File("C://TestUploadCommunity.txt");
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
} catch (Exception e) {
//TODO
}
fileEntry = fileService.updateCommunityFile(fis, fileEntry.getFileId(), fileEntry.getLabel(), communityLibraryId, params);
Can you share more details on your sample, what exactly is your fis?
I have tried this on 2 environments and I do not see any issue.
Also, from the entry you have pasted,
"Request to url apps.na.collabserv.com/files/basic/api/library... /document/... /entry?content-length=6600&createVersion=false returned an error response 411:Length Required HTTP/1.1 411"
It seems that somehow an incorrect content-length is passed for your request.
Can you share the sample that you are using?

GWT Spring Jasper Reports

I have an application built in GWT and Spring. I am trying to generate Jasper Reports on the server side. However when I execute the functionality, it hangs/stops at jasperDesign = JRXmlLoader.load(file_name); and does not respond or throw an exception. This means that my RPC call that triggers the report generation function does not return a response either (so the application hangs). However when I run the function in a normal java application it generates a report without any problem. What could be the issue? I am using JasperReports version 5.6.0. My java function:
public StandardServerResponse printReport(List<Object> items) {
StandardServerResponse response = new StandardServerResponse();
String file_name = null;
Map<String, Object> parameters;
JasperDesign jasperDesign;
JasperReport jasperReport;
JasperPrint jasperPrint;
try {
for (Object obj: items) {
parameters = new HashMap<String, Object>();
parameters.put("id_in", obj.getId());
file_name = "G:\\myreport.jrxml";
jasperDesign = JRXmlLoader.load(file_name); //application stops here
jasperReport = JasperCompileManager.compileReport(jasperDesign);
jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, dataSource.getConnection());
JasperExportManager.exportReportToPdfFile(jasperPrint, "G:\\report.pdf");
}
response.setSuccess(true);
} catch (Exception ex) {
ex.printStackTrace();
response.setSuccess(false);
}
return response;
}
I finally solved my problem after many long days of debugging :-).
I had these two jars in my WEB-INF/lib folder.
jasperreports-functions-5.6.0-SNAPSHOT.jar
jasperreports-fonts-5.6.0.jar
I removed them and the app worked. I still don't understand why they would cause a problem though.
I also changed my code to work with a .jasper extension and directly called JasperRunManager.runReportToPdfFile(file_name, "S:\\output_report.pdf", parameters, connection);
Thanks a lot Darshan Lila for trying, I really appreciate. Hope this helps someone.

IsolatedFileStorage XML Reading Crash

Ok so, basically my problem is with reading and XML file from IsolatedFileStorage. I'll go through the process that leads to my error and then I'll list the relevant code and XML file.
On the first execution it recognises that the file does not exist - it therefore creates the file in IsolatedFileStorage
On the second execution it can now see that the file does exist and so it loads the XML file
On the third execution it can see that it exists - but it throws an XML error
I cannot for the life of me find a solution to it (link to other discussion on MSDN here)
So the code for reading/creating the XML file in IsolatedFileStorage is as follows:
try
{
/***********************
* CHECK THE SETTINGS
********************/
if (store.FileExists("AppSettings.xml"))
{
streamSettings = new IsolatedStorageFileStream("AppSettings.xml", System.IO.FileMode.Open, store);
DebugHelp.Text = "AppSettings.xml exists... Loading!";
streamSettings.Seek(0, System.IO.SeekOrigin.Begin);
xmlDoc = XDocument.Load(streamSettings, LoadOptions.None);
}
else
{
streamSettings = new IsolatedStorageFileStream("AppSettings.xml", System.IO.FileMode.Create, store);
DebugHelp.Text = "AppSettings.xml does not exist... Creating!";
xmlDoc = XDocument.Load("AppSettings.xml", LoadOptions.None);
}
if (xmlDoc != null)
xmlDoc.Save(streamSettings);
}
catch (Exception e)
{
DebugHelp.Text = e.ToString();
}
finally
{
streamSettings.Close();
}
And the related XML file is as follows:
<?xml version="1.0" encoding="utf-8" ?>
<Settings>
</Settings>
Extremely advanced I know - however it throws the following error (here) and you can find the full error text at the bottom of the Social.MSDN page.
Please help - I have been looking for a solution (as the one on the social.msdn site didn't work) for about 2 weeks now.
Why don't you try to read file using a simple StreamReader ? Below a part of a method I have created to readfile from store. Have a try, check your content, and then try loading xml from String (XDocument.Parse etc ...)
String fileContent = String.Empty;
using (_store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (_store.FileExists(file))
{
_storeStream = new IsolatedStorageFileStream(file, FileMode.Open, _store);
using (StreamReader sr = new StreamReader(_storeStream))
{
fileContent = sr.ReadToEnd();
}
__storeStream.Close();
return fileContent;
}
else {
return null;
}
}
It looks to me like the problem is in your save method - it looks like you are maybe appending the settings each time you close - to overwrite your existing settings, you need to ensure that you delete your existing file and create a new one.
To help debug this, try using http://wp7explorer.codeplex.com/ - this might help you see the raw file "on disk"
As an aside, for settings in general, do check out the AppSettings that IsolatedStorage provides by default - unless you have complicated needs, then these may suffice on their own.
Your code sample isn't complete so it's hard to say for sure but, rather than just seeking to the start of the file you may find it easier to just delete it if it already exists. You can do this with FileMode.Create. In turn this means you can do away with the need to check for the existing file.
I suspect that the problem is that you are writing a smaller amount of text to the file on subsequent attempts and so leaving part of the original/previous text behind. In turn this creates a file which contains invalid XML.

Resources