title: How can I save prestashop invoices automatically and manually?
body: <p><strong>I want to print out an invoice(pdf) automatically, what's recently saved on the server. And also making manual saving possible</strong></p>
  
  <p>I'm using prestashop 1.6.1 and the invoices are mostly downloaded from the prestashop admin page, but I needed more easier way to print out these invoices, so I made an adminpage for myself it looks like this:
  <a href="https://i.stack.imgur.com/QXdPC.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QXdPC.png" alt="enter image description here"></a></p>
  
  <p>The printer button has a href of the invoice generated address
  like:"<em><a href="http://www.example.com/admin/index.php?controller=AdminPdf&amp;submitAction=generateInvoicePDF&amp;id_order=3230" rel="nofollow noreferrer">http://www.example.com/admin/index.php?controller=AdminPdf&amp;submitAction=generateInvoicePDF&amp;id_order=3230</a>"</em></p>
  
  <p>From the link I can download it and then print it when it's opened in pdf reader, but I want to do this in one click.</p>
  
  <p>Soo... I made an script for automatically printing the pdf when its saved on some specific location</p>
  
  <pre><code>    #! /usr/bin/python import os
  import os
  import time
  import os.path
  from watchdog.observers import Observer
  from watchdog.events import FileSystemEventHandler
  
  
  
  class ExampleHandler(FileSystemEventHandler):
      def on_created(self, event): 
      output=str(event.src_path.replace("./",""))
      print(output)
          #print event.src_path.replace("./","")
          print "Got event for file %s" % event.src_path
      os.system("lp -d HL2250DN %s" % output)
  
  observer = Observer()
  event_handler = ExampleHandler() 
  observer.schedule(event_handler, path='.',recursive=False)
  observer.start()
  
  try:
      while True:
          time.sleep(1)
  except KeyboardInterrupt:
      observer.stop()
  
  observer.join()
  </code></pre>
  
  <h2>There is two options to download it automatically to the server</h2>
  
  <p><strong>1. Override PDF.php and PDFGenerator.php files like this:</strong></p>
  
  <p><strong>PDF.php</strong>
        
  
  <pre><code>class PDF extends PDFCore
  {
      public function render($display = true)
      {
          if($this-&gt;template == PDF::TEMPLATE_INVOICE)
              parent::render('F', true);
  
          return parent::render($display);
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>PDFGenerator.php</strong></p>
  
  <pre><code>    &lt;?php
  
   class PDFGenerator extends PDFGeneratorCore
  {
      public function render($filename, $display = true)
      {
          if (empty($filename)) {
              throw new PrestaShopException('Missing filename.');
          }
  
          $this-&gt;lastPage();
  
          if ($display === true) {
              $output = 'D';
          } elseif ($display === false) {
              $output = 'S';
          } elseif ($display == 'D') {
              $output = 'D';
          } elseif ($display == 'S') {
              $output = 'S';
          } elseif ($display == 'F') {
              $output = 'F';
              $filename = '/folder/for/print_it/'.str_replace("#", "", $filename);
          } else {
              $output = 'I';
          }
  
          return $this-&gt;output($filename, $output);
      }
  }
  
  ?&gt;
  </code></pre>
  
  <p><strong>2. Use script to download</strong></p>
  
  <h2>First attempt</h2>
  
  <p>The first option worked for automatic saving, but when I tried to save invoices manually I got an blank or broken pdf file. I also tried to change the pdf.php, but it dident work out for me. Also made an post about this: <a href="https://stackoverflow.com/questions/43544165/prestashop-saving-invoices-mannualy-and-automatically">Prestashop saving invoices manually and automatically</a>. No answers were given and I moved on second option.</p>
  
  <h2>Second attempt</h2>
  
  <p>I tried to download invoices with python script and it worked, but how can I know which one to download?</p>
  
  <pre><code>    #!/usr/bin/env python
  import requests
  import webbrowser
  
  url = "http://www.example.com/admin/index.php?controller=AdminLogin&amp;token=5a01dc4e606bca6c26e95ddea92d3d15"
  url2 = "http://www.example.com/admin/index.php?controller=AdminPdf&amp;token=35b276c05aa6f5eb516737a8d534eb66&amp;submitAction=generateInvoicePDF&amp;id_order=3221"
  payload = {'example': 'example',
      'example': 'example',
      'stay_logged_in':'2',
      'submitLogin':'1',}
  
  with requests.session() as s:
      # fetch the login page
      s.get(url)
  
      # post to the login form
      r = s.post(url, data=payload)
      print(r.text)
  
      response = s.get(url2)
  
      with open('/tmp/metadataa.pdf', 'wb') as f:
      f.write(response.content) 
  </code></pre>
  
  <p>Soo the problem with this option is that.. How can I pass the href(what was clicked from the printer button) to url?</p>
  
  <p>Solving this has been really frustrating and I know there is an simple and easy option for this, but I'm still looking for this.</p>