Convert PDF to Base64

SUGGESTED

I would like to send a PDF file to a external Web service, but I could not do it, so I know there is a possibility to send PDF  as base64 to WS, so the webservice decode this base64 in a PDF file.

I know how to decode a clob record in base64, but not how I do that with a PDF file, so my question is that, how I convert a PDF file in Base64 on X3?

If someone knows any alternative to that, please feel free to tell me

  • 0

    Hello, had you find the answer of your question ?

  • 0
    SUGGESTED

    Local Integer LENGTH
    Local Blbfile PDF_BLB(10)
    Local Clbfile PDF_CLB(8)
    Local Char PDF_PATH(GDIMFIC) : PDF_PATH="C:\pdf_file.pdf"

    Openi [L]PDF_PATH Using [PDF]
    Getseq 10,[L]PDF_BLB Using [PDF]
    Openi Using [PDF] 

    [L]LENGTH=b64Encode([L]PDF_BLB, [L]PDF_CLB)
    If [L]LENGTH>0
          Infbox "Encoded in base64"
    Else
          Call ERREUR("Error encoding file.") From GESECRAN
    Endif

  • 0

    Hi Luiz

    You did mention that you know how to decode a clob record. I want to convert Base64 to PDF.

    I'm receiving a Base64 in XML and want to convert that to a PDF. Is that possible?

  • 0 in reply to Chris le Roux9109
    SUGGESTED

    Hello Chris,

    You can use b64Decode to convert clbfile variable into blbfile and then you can create a file with that blbfile variable. For example:

    Local Blbfile YPDFDEST(13)
    Local Clbfile YPDFCLB(8)
    Local Integer YLENGTH
    YPDFCLB = "..." : # Your base64 string
    
    #--- Decode base 64 (Convert base 64 string to binary data)
    [L]YLENGTH=b64Decode([L]YPDFCLB, [L]YPDFDEST)
    Infbox "Length decode base 64 = "+num$([L]YLENGTH)
    
    #--- Write into PDF file
    Local char YFILEPATH_DEST(250) : YFILEPATH_DEST=filpath("TMP","","")+"\test_"+num$(time)+".pdf" : # Your final PDF file path
    Openo [L]YFILEPATH_DEST Using [WRT] : # Open file
    Putseq 1,[L]YPDFDEST Using [WRT] : # Inject binary data into file
    Openo Using [WRT] : # Close file
    
    If filinfo(YFILEPATH_DEST,7)>0
        Infbox "PDF created @ "+ctrans(YFILEPATH_DEST,"\","/")
    endif

  • 0 in reply to RobertoMSNascimento

    Hi Roberto

    This is working thank you. However, my client is sending a sting of almost 1 800 000 characters. As far as I know, a Sage web service can only handle up to 65 000 characters. Can I split the base64 string into pieces of 65 000 characters and have a Clbfile with dimensions?

  • 0 in reply to Chris le Roux9109

    Hello,

    I do not think this should be done to transfer such large files... specially since if you have a variable with the content it means it will load that content into the X3 session process memory and it will probably blow up... also webservice call might even timeout on the other side depending on network speeds and timeout setting.

    Is FTP not an option? It is possible to develop a module in NodeJS to transfer files through FTP by calling func ASYRWEBSER.EXEC_JS(). In this case, files can be transferred between servers without eagerly loading into memory and if you need a webservice to trigger an action after file is in FTP, you can have a webservice that receives the file path on the FTP and then call the library to download the file.

    Anyway this thread is already going a little bit off topic... I suggest you open another topic if this idea is not enough.