CasperJS - Download a PDF file

1

Good afternoon, I would like to download a PDF file by clicking a button that submits a form using POST. In this case I have no URL to use the casper download method. Please see the HTML code below:

<html>
<head>
<title>Boletim</title>
</head>
<body>
<form method="post" action="boletim.aspx" id="form1">
<div class="aspNetHidden">
   <input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value=""/>
   <input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
   <input type="hidden" name="__LASTFOCUS" id="__LASTFOCUS" value="" />
   <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/MVzBQRB" />
</div>
<script type="text/javascript">
//<![CDATA[
var theForm = document.forms['form1'];
if (!theForm) {
   theForm = document.form1;
}
function __doPostBack(eventTarget, eventArgument) {
   if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
      theForm.__EVENTTARGET.value = eventTarget;
      theForm.__EVENTARGUMENT.value = eventArgument;
      theForm.submit();
   }
}
//]]>
</script>
<script src="/fac/WebResource.axd?d=ywLVQ1&amp;t=6353" type="text/javascript"></script>
<div class="aspNetHidden">
   <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="12310CFB" />
   <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEg73380Az+rg==" />
</div>
<div>
<span id="WebReport1">
   <table width="100%" height="28" bgcolor="#ECE9D8" border="0">
       <tr height="19" valign="middle" align="center">
         <td width="125" align="left">
        <select name="WebReport1$ctl05" title="Export" style="font-family:Tahoma;font-size:8pt;">
               <option selected="selected" value="pdf">Adobe Acrobat</option>
               <option value="csv">CSV file</option>
               <option value="txt">Text File</option>
            </select>
         </td>
         <td width="16">
            <input type="image" name="WebReport1$ctl07" title="Export" src="/fac/WebResource.axd?d=-Yz4rC1&amp;t=5312" />
         </td>
      </tr> 
   </table>
</span>            
</div>
<script type=text/javascript>
    function frxOpenPrint(url){window.open(url, '_blank','resizable,scrollbars,width=700px,height=500px');};
</script>
</form>
</body>
</html>

I would like to click on the image for the input called "WebReport1 $ ctl07" to submit the form called "form1" and download the PDF file.

How to do this?

I tried the casperjs code below, but although it does not show any errors, the download does not work.

// Select file type to download 
casper.then(function(){
   this.fillSelectors('form#form1', {
      'select[name="WebReport1$ctl05"]' : "pdf"
   }, false);
   this.wait(1000,function(){  
      this.echo("Select OK");
   }); 
}); 
// Download PDF file - not run
casper.then(function(){
   this.click('input[type="image"][name="WebReport1$ctl07"]');
   this.page.onFileDownload = function(status){
      console.log('onFileDownload(' + status + ')');
      return "rl_boletim.pdf";
   };
});

Any suggestions?

Thank you Adriano

    
asked by anonymous 06.05.2016 / 22:35

0 answers