Is it possible to read pdf file from url ex : "http://site.com/thispdf"

Just want to ask, is it possible to read pdf document but the link is
http://site.com/thispdf

instead of

http://site.com/thispdf.pdf

this pdf is generated automatically so it’s not saved or stored on web storage.
If anyone have solution please answer my question. Thank You.

Hi Didit,

I am afraid that you should need some 3rd party Java library to read PDF files.

You want to download it and parse/read afterwards? Or what do you want to do with the file?

Marek Melocik said:

Hi Didit,

I am afraid that you should need some 3rd party Java library to read PDF files.

You want to download it and parse/read afterwards? Or what do you want to do with the file?

already using java 3rd library, pdfbox
the problem is cannot open stream(BufferedInputStream) because server detect unauthorized access(it requires authentication header), (othercase)using httpurlconnection, we can set property like this httpConn.setRequestProperty(“Authorization”, basicAuth) to pass header authentication, is there any option for openstream like setrequestproperty on openconnection

I have the same problem. Do you have any solusion?

HI,

are you able to save it to your pc?
you can try to use browser properties for that, which browser used?

not sure, but something like that

https://www.toolsqa.com/selenium-webdriver/how-to-download-files-using-selenium/

Hi,
disable chrome browser pdf viewer option from the chrome setting.

Testcase

//set chrome options
driver = setChromeOptions(folder)
DriverFactory.changeWebDriver(driver)

//verify pdf
driver.get('https://pressbooks.com/sample-books/')
driver.findElement(By.xpath("//article[@id='post-2344']/div/ul/li/a")).click()
def url = WebUI.getUrl()
println url
String pdfContent = CustomKeywords.'readPdfFile.verifyPdfContent.readPdfFileVerify'(url)
Assert.assertTrue(pdfContent.contains('The PressBooks version of The Metamorphosis, by Franz Kafka.'))

public WebDriver setChromeOptions(File folder){
	
	ChromeOptions optionsBeta = new ChromeOptions();
	String downloadPath = folder.getAbsolutePath()
	//String downloadsPath = System.getProperty("user.home") + "/Downloads";
	println ("downloadpath "+downloadPath)
	
	Map<String, Object> chromePrefs = new HashMap<String, Object>()
	chromePrefs.put("profile.default_content_settings.popups", 0);
	chromePrefs.put("download.default_directory", downloadPath)
	chromePrefs.put("download.prompt_for_download", false)
	chromePrefs.put("plugins.plugins_disabled", "Chrome PDF Viewer");
	
	optionsBeta.setExperimentalOption("prefs", chromePrefs)
	DesiredCapabilities cap = DesiredCapabilities.chrome()
	cap.setCapability(ChromeOptions.CAPABILITY, optionsBeta)
	
	System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
	WebDriver driver = new ChromeDriver(cap);
	return driver
}

Keyword

	@Keyword
	public String readPdfFileVerify(String pdfUrl){

		URL TestURL = new URL(pdfUrl);
		BufferedInputStream bis = new BufferedInputStream(TestURL.openStream());
		PDDocument doc = PDDocument.load(bis);
		String pdfText = new PDFTextStripper().getText(doc);
		doc.close();
		bis.close();
		println(pdfText);
		return pdfText;

	}

Hi,

first disable pdf viewer option from chrome settings.

Testcase

File folder

folder = new File(UUID.randomUUID().toString())
folder.mkdir()

//set chrome options
driver = setChromeOptions(folder)
DriverFactory.changeWebDriver(driver)

//verify pdf
driver.get('https://pressbooks.com/sample-books/')
driver.findElement(By.xpath("//article[@id='post-2344']/div/ul/li/a")).click()
def url = WebUI.getUrl()
println url
String pdfContent = CustomKeywords.'readPdfFile.verifyPdfContent.readPdfFileVerify'(url)
Assert.assertTrue(pdfContent.contains('The PressBooks version of The Metamorphosis, by Franz Kafka.'))

public WebDriver setChromeOptions(File folder){
	
	ChromeOptions optionsBeta = new ChromeOptions();
	String downloadPath = folder.getAbsolutePath()
	println ("downloadpath "+downloadPath)
	
	Map<String, Object> chromePrefs = new HashMap<String, Object>()
	chromePrefs.put("profile.default_content_settings.popups", 0);
	chromePrefs.put("download.default_directory", downloadPath)
	chromePrefs.put("download.prompt_for_download", false)
	chromePrefs.put("plugins.plugins_disabled", "Chrome PDF Viewer");
	
	optionsBeta.setExperimentalOption("prefs", chromePrefs)
	DesiredCapabilities cap = DesiredCapabilities.chrome()
	cap.setCapability(ChromeOptions.CAPABILITY, optionsBeta)
	
	System.setProperty("webdriver.chrome.driver", DriverFactory.getChromeDriverPath())
	WebDriver driver = new ChromeDriver(cap);
	return driver
}

Keyword

	@Keyword
	public String readPdfFileVerify(String pdfUrl){

		URL TestURL = new URL(pdfUrl);
		BufferedInputStream bis = new BufferedInputStream(TestURL.openStream());
		PDDocument doc = PDDocument.load(bis);
		String pdfText = new PDFTextStripper().getText(doc);
		doc.close();
		bis.close();
		println(pdfText);
		return pdfText;

	}