I am trying to extract pdf attachment from business central through a custom api
I created an API page for the attachment document table but the pdf documents are not accessible.
hi
Create a custom API page for the Attachment table. In the API page, you should include the fields you need to retrieve the PDF attachment, such as Attachment ID, Attachment Name, and Attachment Data.
In the API page, add a codeunit that retrieves the Attachment Data field for the PDF attachment. You can use the "Attachment Management" codeunit to do this.
Modify the "OnGetRecord" trigger in the API page to call the codeunit that retrieves the Attachment Data field. For example, you can add the following code to the "OnGetRecord" trigger:
Trigger OnGetRecord()
Var
AttachmentMgt: Codeunit "Attachment Management";
Begin
AttachmentMgt.GetAttachmentStream(Attachment.ID, Attachment."File Name", PDFStream);
End;
In the above code, "AttachmentMgt" is the codeunit variable that references the "Attachment Management" codeunit. "PDFStream" is a variable that holds the stream of the PDF attachment.
Return the PDF attachment as the response of the API call. You can use the "SendStream" function to do this. For example, you can add the following code to the "OnAfterGetRecord" trigger:
Trigger OnAfterGetRecord()
Begin
Response.ContentDisposition := 'attachment;filename="' + Attachment."File Name" + '"';
Response.ContentType := 'application/pdf';
Response.SendStream(PDFStream);
End;
In the above code, "Response" is the response variable that is returned by the API page. "ContentDisposition" and "ContentType" are the HTTP headers that specify the file name and content type of the PDF attachment. "SendStream" sends the PDF stream as the response of the API call.
DAniele
Hi,
You can convert pdf to base64 text and send the base64 text to endpoint.
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,228 Super User 2024 Season 2
Martin Dráb 230,056 Most Valuable Professional
nmaenpaa 101,156