Watir Won't Download PDF, Only Opens in Viewer

Read dynamic PDF from Ruby Watir

I figured out a solution.

In the profile for firefox you can set the plugin.scan.Acrobat to "999" which will effectively disable the PDF plugin.

    profile = Selenium::WebDriver::Firefox::Profile.new
profile['plugin.scan.Acrobat'] = "999"
b = Watir::Browser.new :firefox, :profile => profile

Opening PDF's using `target=blank` fails on Android Chrome

Make the URL relative to the site domain

<a href="/sample.pdf" target="_blank">Open PDF</a> 
<!-- Not <a href="/example.com/sample.pdf>Open PDF</a> -->

This allowed the links to download without issue on the Google Chrome app (specifically on Samsung).

downloading a file that comes as an attachment in a POST request response in PhantomJs

I've found a way to do this using casperjs (it should work with phantomjs alone if you implement the download function using XMLHttpRequest, but i've not tried).

I'll leave you the working example, that tries to download the mos recent PDF from this page. When you click the download link, some javascript code is triggered that generates some hidden input fields that are then POSTed.

What we do is replace the form's onsubmit function so that it cancels the submission, and get the form destination (action) and all its fields. We use this information later to do the actual download.

var casper=require('casper').create();
casper.start("https://sede.gobcan.es/tributos/jsf/publico/notificaciones/comparecencia/ultimosanuncios.jsp", function() {

var theFormRequest = this.page.evaluate(function() {
var request = {};
var formDom = document.forms["resultadoUltimasNotif"];
formDom.onsubmit = function() {
//iterate the form fields
var data = {};
for(var i = 0; i < formDom.elements.length; i++) {
data[formDom.elements[i].name] = formDom.elements[i].value;
}
request.action = formDom.action;
request.data = data;
return false; //Stop form submission
}

//Trigger the click on the link.
var link = $("table.listado tbody tr:first a");
link.click();

return request; //Return the form data to casper
});

//Start the download
casper.download(theFormRequest.action, "downloaded_file.pdf", "POST", theFormRequest.data);
});

casper.run();

Note: you have to run it with --ignore-ssl-errors, as the CA they use isn't in your browser default CA list.

casperjs --ignore-ssl-errors=true downloadscript.js


Related Topics



Leave a reply



Submit