Getting an End of File error when validating PDFs in Katalon Studio? Learn why dynamic PDF URLs cause it and how to fix it by downloading the file locally before reading it with PDFBox.
Hello, Katalon users and software testing enthusiasts! Today, we're looking at a common issue discussed in the Katalon Community: the "End of File" exception when validating PDFs.
The Issue at Hand
One of our community members, ragrawal, recently encountered an issue while validating PDFs using Katalon Studio. They were getting an ‘End of File, expected Line Error’ despite downloading the necessary JARs through External Libraries in Katalon Studio.
Debugging the Issue
The first step suggested by Timo_Kuisma, another community member, was to debug and print out what was being passed to PDFTextStripper. However, ragrawal was still getting the 'End of File Error’.
The Root Cause
Upon further investigation, it was discovered that the issue was due to the dynamic URL of the PDF. The last ID in the URL kept changing whenever the script was run. This led to the PDF file not being read properly.
The Solution
Timo_Kuisma suggested a workaround: download the PDF first to the local machine, then load it from the path where it’s downloaded. This approach ensures that the PDF file is read properly, thus avoiding the 'End of File Error’.
Implementing the Solution in Code
Here’s a sample code snippet that implements the solution:
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.nio.file.StandardCopyOption
import org.apache.pdfbox.pdmodel.PDDocument
import org.apache.pdfbox.text.PDFTextStripper
import com.kms.katalon.core.webui.keyword.WebUiBuiltInKeywords as WebUI
// Capture the current (dynamic) PDF URL at runtime
String pdfUrl = WebUI.getUrl()
WebUI.comment(pdfUrl)
// Download the PDF to a local file first
Path pdfPath = Paths.get(System.getProperty('java.io.tmpdir'), 'downloaded.pdf')
new URL(pdfUrl).openStream().withCloseable { input ->
Files.copy(input, pdfPath, StandardCopyOption.REPLACE_EXISTING)
}
// Load the local copy and extract its text (PDFBox 2.x)
PDDocument document = PDDocument.load(pdfPath.toFile())
try {
String pdfContent = new PDFTextStripper().getText(document)
println(pdfContent)
} finally {
document.close()
}This script captures the PDF's URL at runtime, saves the file to a local temp folder, and then loads the local copy with PDFBox, so the dynamic URL can't change mid-read. It uses the PDFBox 2.x API; if you've added PDFBox 3.x to External Libraries, replace PDDocument.load(...) with Loader.loadPDF(...) (import org.apache.pdfbox.Loader). If the PDF sits behind a login, the download may return an HTML page instead of a PDF, which also triggers an End of File error, so check the saved file if the problem persists.
Conclusion
We hope this blog post has provided you with a deeper understanding of how to handle the ‘End of File Error’ exception when validating PDFs with Katalon Studio. Remember, our Katalon Community is a great resource for troubleshooting and learning from the experiences of other Katalon users.
The information in this blog post is based on a real-life scenario shared by a user on our Katalon Community forum and is intended to inspire peer-to-peer discussion and collaboration. Please always test solutions thoroughly before implementing them in a production environment.
Feel free to continue the discussion on the Katalon forum.

Test everything. Ship with confidence.
From generating test cases to spotting failures, Katalon's AI works across your entire pipeline, automatically.





