[SalesForce] Converting Attached PDF into base64

Currently I am letting user to upload multiple PDF Files. Now I have created a service outside of salesforce that will merge multiple PDF into one. I need to convert those PDF Files into base64 in order to send them into the service files. can I convert the pdf files into base54 without inserting them into Document Object?

public void save()
{
        List<Attachment> toInsert=new List<Attachment>();
        for (Attachment newAtt : newAttachments)
        {
            if (newAtt.Body!=null)
            {
                newAtt.parentId=sobjId;
                newAtt.OwnerId = UserInfo.getUserId();
                toInsert.add(newAtt);
            }
        }
        insert toInsert;
        newAttachments.clear();
        newAttachments.add(new Attachment());


        attachments=null;
}

Component

<apex:component controller="MultiAttachmentController" allowDML="true">
    <apex:attribute name="objId" type="String" description="The id of the object to manage attachments for" required="true" assignTo="{!sobjId}"/>
    <apex:form id="attForm">
        <apex:pageBlock title="Upload Attachments">
            <apex:repeat value="{!newAttachments}" var="newAtt">
                <apex:pageBlockSection columns="3">
                   <apex:pageBlockSectionItem >
                        <apex:outputLabel value="File"/>                         
                        <apex:inputFile value="{!newAtt.body}" filename="{!newAtt.name}"/>
                    </apex:pageBlockSectionItem>
                    <apex:pageBlockSectionItem >
                        <apex:outputLabel value="Description"/>                      
                        <apex:inputText value="{!newAtt.Description}"/>
                    </apex:pageBlockSectionItem>
                </apex:pageBlockSection>
            </apex:repeat>
            <apex:commandButton value="Add More" action="{!addMore}"/>
            <apex:commandButton value="Save" action="{!save}"/>
            <apex:commandButton value="Done" action="{!done}"/>
        </apex:pageBlock>
    </apex:form>
    <apex:pageBlock title="Existing Attachments">
        <apex:pageBlockTable value="{!attachments}" var="attachment">
            <apex:column headerValue="Action">
               <apex:outputLink value="{!URLFOR($Action.Attachment.Download, attachment.Id)}" target="_blank">View</apex:outputLink>
            </apex:column>
            <apex:column value="{!attachment.Name}"/>
            <apex:column value="{!attachment.Description}"/>
        </apex:pageBlockTable>
        <apex:pageBlockTable value="{!documents}" var="document">
            <apex:column >
                <apex:outputLink value="/{!document.Id}">{!document.Name}</apex:outputLink>
            </apex:column>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:component>

Best Answer

document object's body field type is base64.

You dont have to convert

If you have string with base64 data.

then use encodingutil.base64decode for convertion

base64Decode(inputString)

  • Converts a Base64-encoded String to a Blob representing its normal form.

base64Encode(inputBlob)

  • Converts a Blob to an unencoded String representing its normal form.

Encodingutil class https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_restful_encodingUtil.htm

Related Topic