function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
pablorpablor 

PDF from Document Object

We are trying to extract a PDF Document object from Salesforce.
We've managed to get the document, but seem to be getting stuck with the encoding.
Please see the VB code below.

It does create a PDF file, and it even has the correct number of pages, but all the pages are blank.

Comparing the original PDF file, and the one we are creating and a text editor, we can see the character coding is slightly different:

The 'good' PDF starts with:
%PDF-1.4

%âãÏÓ

The 'bad' one is:
%PDF-1.4

%âã�Ó

We've tried all sorts of decoding but are completely stuck.
Anyone any ideas?

 

Thanks - Pablo

 

Public Partial Class pdfSave
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim mySF As New MySalesForce.SforceService

        'Logon to our instance of Salesforce
        Logon(mySF)

        'Get the Document Object
        Dim mySQL As String = "SELECT name, body, BodyLength, Type  FROM Document WHERE id = '015d0000000yxsw'"

        Dim myQueryResult As MySalesForce.QueryResult = mySF.query(mySQL)
        Dim myDoc As MySalesForce.Document = myQueryResult.records(0)

        'Convert the Body form Base64???? to a string
        Dim myStr As String = ""
        For i = 0 To myDoc.BodyLength - 1
            myStr = myStr & Chr(myDoc.Body(i))
        Next

        'Open a file streamwriter (on your desktop - change this for your own desktop)
        Dim myStreamWriter As New System.IO.StreamWriter("C:\Users\JB\Desktop\test.pdf")

        'Write the PDF to the file
        myStreamWriter.Write(myStr)

        myStreamWriter.Close()


    End Sub

    Protected Sub Logon(ByRef paSF As MySalesForce.SforceService)

        Dim myLoginResult As MySalesForce.LoginResult
        Dim myToken As String = ""

        Dim myUsername As String = "pablo@example.com"
        Dim myPassword As String = String.Format("{0}{1}", "pass", "keytoken")

        myLoginResult = paSF.login(myUsername, myPassword)

        paSF.Url = myLoginResult.serverUrl
        paSF.SessionHeaderValue = New MySalesForce.SessionHeader
        paSF.SessionHeaderValue.sessionId = myLoginResult.sessionId

    End Sub

End Class

Best Answer chosen by Admin (Salesforce Developers) 
pablorpablor

We've solved the problem.

The secret is to set the encoding to System.Text.Encoding.Default

 

I'd have thought that leaving it blank would select Default - but clearly its not that simple!

Here is the full line we used:

 

Dim myStreamWriter AsNew System.IO.StreamWriter("D:\SalesForce\ShowOrder\SaveTest.pdf", False, System.Text.Encoding.Default)

 

Pablo