Class: Dolibarr::Documents

Inherits:
Resource show all
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

Methods inherited from Resource

#initialize

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.

Parameters:

  • ref (String)

    the invoice reference (e.g. "FA2601-0007")

  • to (String)

    destination file path

Returns:

  • (String)

    the destination path written

Raises:



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