How can I return a PDF file from server using restful api

Explorer ,
Jun 16, 2015 Jun 16, 2015

Copy link to clipboard


Wondering how to return a PDF file from the server using the restful api.

A document id is provided, which retrieves a record that has the file path and file name information.

From there the file is retrieved into a variable.

I have tried returning as Binary Base64 with a returntype = binary in the api

<cfcomponent restpath="document" rest="true" output="false" extends="cfc.document">

     <cffunction name="getDocument" access="remote" output="false" httpmethod="get" returntype="binary" restpath="view/{documentId: \d+}" >

          <cfargument name="documentId" required="true" type="numeric" restargsource="path">

          <cfset REQUEST.sessionObj = REQUEST.securityObj.checkSession(REQUEST.sessionKey)>

          <cfset LOCAL.documentData = getDocumentData(REQUEST.sessionObj.userId, arguments.documentId, REQUEST.sessionObj.locale, REQUEST.sessionObj.languageId)>

          <cfreturn documentData>



Next file cfc.document

<cfcomponent name="document" output="false" extends="cfc.metadata.document">

     <cffunction name="getDocumentData" output="no" returntype="any">

          <cfargument name="userId" type="numeric" required="yes">

          <cfargument name="documentId" type="numeric" required="yes">

          <cfargument name="locale" type="string" required="yes">

          <cfargument name="languageId" type="numeric" required="yes">

          <cfset pdfFileContent = fileRead("C:\PDFFiles\PDFSample.pdf")>

          <cfset documentData = ToBinary(ToBase64(pdfFileContent))>

          <cfreturn documentData>









Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
no replies

Have something to add?

Join the conversation