We can retrieve any report of sales and purchase documents as a PDF document using Business central URLs.
Let’s explore its functionality in the Business Central SaaS version.
First, we need to generate the complete API URL to access our posted sales and purchase invoices.
You can learn more from here how to get PDFdocument using Business Central API
To access the URLs for these invoices, we must retrieve our Business Central endpoints. Please refer below for the details. You can learn more about this here: BC Endpoints
HTTP request
GET Method : https://{businesscentralPrefix}/api/v2.0/companies({id})
Request headers
Header | Value |
---|---|
Authorization | Bearer {token}. Required. |
Request body
Do not supply a request body for this method.
To get the company {ID} as mentioned in the HTTP request we can use the below URLs. using Bearer Token
https://api.businesscentral.dynamics.com/v2.0/{Tenant ID}/{EnvironmentName}/api/v2.0/companies
You will get the below response and copy the id as marked in the Image.
and use this URL if you want to access any specific company by using that ID with GET method – https://api.businesscentral.dynamics.com/v2.0/{Tenant ID}/{EnvironmentName}/api/v2.0/companies(40987a73-eb3b-ee11-bdf5-00224897e30d)
We have now generated our endpoints using the specific company ID. With these endpoints, we will retrieve the posted purchase invoice document using its unique ID. This will enable us to create the final URL required to obtain the report in PDF format.
To retrieve the SystemID of a posted purchase invoice, please note that every table in Business Central is assigned a Unique ID when a record is created, stored in the SystemID field. You can either directly copy this ID or regenerate the URL to access the Purchase Invoice data.
To obtain the data of Posted Purchase Invoices and access the SystemID field, use the following URL:
https://api.businesscentral.dynamics.com/v2.0/{Tenant ID}/{EnvironmentName}/api/v2.0/companies(40987a73-eb3b-ee11-bdf5-00224897e30d)/purchaseInvoices
Alternatively, you can use the “Page Inspect” feature within Business Central for the corresponding record and copy the value of the SystemID field as illustrated in the image below.
After copying the value of the above SystemID field, our endpoint for retrieving the report as a PDF document should appear as follows:
https://api.businesscentral.dynamics.com/v2.0/{TenantID}/{EnvironmentName}/api/v2.0/companies(40987a73-eb3b-ee11-bdf5-00224897e30d)/purchaseInvoices(da4ca8ec-8be5-ed11-8848-002248979d18)
Lets use this URL to get the report as a PDF by adding /pdfDocument/pdfDocumentContent keywords inthe URL, I am using here the Postman to show you how the PDF document will like
https://api.businesscentral.dynamics.com/v2.0/{TenantID}/{EnvironmentName}/api/v2.0/companies(40987a73-eb3b-ee11-bdf5-00224897e30d)/purchaseInvoices(da4ca8ec-8be5-ed11-8848-002248979d18)/pdfDocument/pdfDocumentContent
By using above URL with GET method you will get below response in Base64 format we have to conver this format or we can save as that response in the PDF format
To Save this Response as a PDF, Follow the Image Guide Below:
Check the PDF file how it look like:
Pretty helpful blog!
Thanks Aditya much appreciate 🙂