java.lang.IllegalStateException: Cannot read while there is an open stream writer

R Dhaval :

I am writing a simple selenium test in which I need to take a screenshot of webpage and save it as PDF. I am using TestNG and PDFbox library with Selenium

Below is my test method:

package com.helper;

import java.io.File;
import java.io.IOException;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.testng.annotations.Test;



public class ScreenshotPDF {

    @Test
    public void screenshotPDF(){

    WebDriver driver= DriverManager.getWebdriver("chrome");

    driver.get("https://www.google.co.in");

    try {
        File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);

        PDDocument document = new PDDocument();
        PDPage page = new PDPage();
        document.addPage(page);
        PDImageXObject pdi = PDImageXObject.createFromFileByContent(screenshot ,document);
        PDPageContentStream contentStream = new PDPageContentStream(document, page);
        contentStream.drawImage(pdi,100,100);

        document.save("C:/Users/123456/Documents/sample.pdf");
        contentStream.close();
        document.close();
    } catch (WebDriverException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }
}
}

Here when I run this test with TestNG, I get the following errors in Console:

FAILED: screenshotPDF
java.lang.IllegalStateException: Cannot read while there is an open stream writer
    at org.apache.pdfbox.cos.COSStream.createRawInputStream(COSStream.java:129)
    at org.apache.pdfbox.pdfwriter.COSWriter.visitFromStream(COSWriter.java:1177)
    at org.apache.pdfbox.cos.COSStream.accept(COSStream.java:372)
    at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObject(COSWriter.java:561)
    at org.apache.pdfbox.pdfwriter.COSWriter.doWriteObjects(COSWriter.java:490)
    at org.apache.pdfbox.pdfwriter.COSWriter.doWriteBody(COSWriter.java:474)
    at org.apache.pdfbox.pdfwriter.COSWriter.visitFromDocument(COSWriter.java:1073)
    at org.apache.pdfbox.cos.COSDocument.accept(COSDocument.java:419)
    at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1331)
    at org.apache.pdfbox.pdfwriter.COSWriter.write(COSWriter.java:1229)
    at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1095)
    at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1067)
    at org.apache.pdfbox.pdmodel.PDDocument.save(PDDocument.java:1055)
    at com.helper.ScreenshotPDF.screenshotPDF(ScreenshotPDF.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
    at org.testng.SuiteRunner.run(SuiteRunner.java:261)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
    at org.testng.TestNG.run(TestNG.java:1048)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:152)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:57)


===============================================
    Default test
    Tests run: 1, Failures: 1, Skips: 0

I did so much of googling but did not find the solution. Please help me with this error. At least what can be the reason of this error. Thank you in advance.

Tilman Hausherr :

You need to close the content stream before saving:

    PDDocument document = new PDDocument();
    PDPage page = new PDPage();
    document.addPage(page);
    PDImageXObject pdi = PDImageXObject.createFromFileByContent(screenshot ,document);
    PDPageContentStream contentStream = new PDPageContentStream(document, page);
    contentStream.drawImage(pdi,100,100);
    contentStream.close(); // do this before saving!

    document.save("C:/Users/123456/Documents/sample.pdf");
    document.close();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

java.lang.IllegalStateException: Unable to read meta-data for class while implementing open api with spring boot

java.lang.IllegalStateException: open

java.lang.IllegalStateException: stream has already been operated upon or closed in Java while working with list

In Java why do i get java.lang.IllegalStateException: cannot open system clipboard

java.lang.IllegalStateException while reloading Tomcat

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling

java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling when notifydatasetChanged()

java.lang.IllegalStateException: InputStream has already been read - do not use InputStreamResource if a stream needs to be read multiple times

Neo4j TestGraphDatabase: Caused by: java.lang.IllegalStateException: Cannot close the PageCache while files are still mapped:

java.lang.IllegalStateException while using Document Listener in TextArea, Java

java.lang.IllegalStateException while trying to retrieve album art

java.lang.IllegalStateException: Circular dependencies cannot exist in RelativeLayout : XML

java.lang.IllegalStateException: Cannot find any build directories

java.lang.IllegalStateException: Ambiguous mapping. Cannot map method

java.lang.IllegalStateException: Cannot perform this operation because there is no current transaction

read tcp stream with java while writing to database

ByteBuddy 1.10.2 throws `java.lang.IllegalStateException: Cannot resolve type description for java.lang.Exception`, and similar

nested exception is java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type

Cannot convert value of type 'java.lang.String' to required type - java.lang.IllegalStateException:

Java 8 stream.map() : Exception in thread "main" java.lang.IllegalStateException: source already consumed or closed

java.lang.NullPointerException while using PeasyCam and Processing. PeasyCam says it cannot read "width" because <parameter2> is null

Exception in thread "main" java.lang.IllegalStateException: stream has already been operated upon or closed

java.lang.IllegalStateException while removing an element from the list based on another list<Double> Java

java.lang.IllegalStateException in .NET?

java.lang.illegalStateException: Butterknife

java.lang.IllegalStateException in retrofit

EJB java.lang.IllegalStateException

java.lang.IllegalStateException Butterknife

Caused by: java.lang.IllegalStateException: