Class: Dolibarr::Documents
- Defined in:
- lib/dolibarr/documents.rb
Overview
Documents: fetch generated files from Dolibarr's document store.
Dolibarr's GET /documents/download returns the file as a JSON envelope with the
bytes base64-encoded (not a raw binary stream), so this decodes and writes them.
Constant Summary collapse
- INVOICE_MODULEPART =
Modulepart Dolibarr uses for customer invoices in its document store.
'facture'
Instance Method Summary collapse
-
#download_invoice_pdf(ref:, to:) ⇒ String
Download a customer invoice's PDF to a local path.
Methods inherited from Resource
Constructor Details
This class inherits a constructor from Dolibarr::Resource
Instance Method Details
#download_invoice_pdf(ref:, to:) ⇒ String
Download a customer invoice's PDF to a local path.
The PDF must already have been generated in Dolibarr (validating the invoice, or opening it in the UI, builds it); if it has not, Dolibarr answers 404 and this raises Client::NotFound.
25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/dolibarr/documents.rb', line 25 def download_invoice_pdf(ref:, to:) envelope = get_one( '/documents/download', "PDF for invoice #{ref}", 'modulepart' => INVOICE_MODULEPART, 'original_file' => "#{ref}/#{ref}.pdf" ) content = envelope['content'] raise Client::Error, "download response for #{ref} carried no content" if content.nil? bytes = envelope['encoding'].to_s == 'base64' ? Base64.decode64(content) : content File.binwrite(to, bytes) to end |