web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Small and medium business | Business Central, N...
Suggested Answer

How do I extract pdf attachment through API

(0) ShareShare
ReportReport
Posted on by 57

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.

I have the same question (0)
  • Suggested answer
    Bilal Haider Profile Picture
    414 on at

    Hi,

    You can convert pdf to base64 text and send the base64 text to endpoint.

  • Suggested answer
    DAnny3211 Profile Picture
    11,397 on at

    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

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Small and medium business | Business Central, NAV, RMS

#1
OussamaSabbouh Profile Picture

OussamaSabbouh 2,468

#2
YUN ZHU Profile Picture

YUN ZHU 923 Super User 2025 Season 2

#3
Sumit Singh Profile Picture

Sumit Singh 607

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans