PxPlus User Forum

Twitter Twitter Twitter

Author Topic: Transform pdf to base 64  (Read 825 times)

marcbr

  • Member
  • **
  • Posts: 12
    • View Profile
Transform pdf to base 64
« on: August 24, 2022, 03:55:55 AM »
Hello,
For communication needs we should convert a pdffile into a base 64file.
We use this code :

 BASE64_FILE$="50_09790301_13_200_002_led_kalender.base64"
 PDFFIL$="50_09790301_13_200_002_led_kalender.pdf"
 ERASE BASE64_FILE$,ERR=*NEXT
 CALL "*web/base64;ENCODE_FILE",PDFFIL$,BASE64_FILE$

The base64 file is created. With copy/paste we put the base64 string on a converter website : https://base64.guru/converter/decode/pdf
The pdf generated from the base64 string is empty!
This is also the result what our customer receives, an empty pdf.

With  instruction CALL "*web/base64;DECODE_FILE",BASE64_FILE$,PDFNEW$  a good readable pdf is created.
We use Pxplus 16.10 on RH6
Thanks for helping




EVa

  • Gold Member
  • ****
  • Posts: 54
    • View Profile
    • EDIAS
Re: Transform pdf to base 64
« Reply #1 on: August 24, 2022, 04:15:08 AM »
I can't reproduce this - here's what I did:

PxPlus-2019 Web (Ver:16.10/UNIX-Linux-RedHat) Serno:1610-664-0xxxxxx
(c) Copyright 2005-2018 PVX Plus Technologies Ltd. (All rights reserved)
  Website: http://www.pvxplus.com
->010 BEGIN
-:0020 LET BASE64_FILE$="/tmp/50_09790301_13_200_002_led_kalender.base64"
-:0030 LET PDFFIL$="/tmp/50_09790301_13_200_002_led_kalender.pdf"
-:0040 OPEN (1)"*PDF*;FILE="+PDFFIL$+";OVERWRITE"; LIST (1); CLOSE (1)
-:0050 ERASE BASE64_FILE$,ERR=*NEXT                                   
-:0060 CALL "*web/base64;ENCODE_FILE",PDFFIL$,BASE64_FILE$           
-:run                                                                 .                     
-:open input(1)base64_file$;print rcd(1)
JVBERi0xLjMKJbe+raoKMSAwIG9iago8PAovVHlwZSAvQ2F0YWxvZwovUGFnZXMgMiAwIFIKPj4KZW5kb2JqCjIgMCBvYmoKPDwKL1R5cGUgL1BhZ2VzCi9LaWRzIFsgNSAwIFIgXQovQ291bnQgMQo+PgplbmRvYmoKMyAwIG9iago8PAovUHJvZHVjZXIgKHd3dy5wdnhwbHVzLmNvbSkKL0F1dGhvciAocm9vdCkKL0NyZWF0b3IgKFB4UGx1cy0xNi4xMC4wMDAwKQovVGl0bGUgKFVudGl0bGVkKQovU3ViamVjdCAoTm8gU3ViamVjdCkKL0tleXdvcmRzIChub25lKQovQ3JlYXRpb25EYXRlIChEOjIwMjIwODI0
-:

I open the base64 file in gedit, copy the contents and paste them on that website.  The result is the attached picture (the correct PDF contents).

marcbr

  • Member
  • **
  • Posts: 12
    • View Profile
Re: Transform pdf to base 64
« Reply #2 on: August 24, 2022, 12:10:58 PM »
Hi EVa,
The base64 file is put in a jsonstring for transmission. Here line feeds are converted to the escape character \n. Apparently the conversion on the receiving side is not done properly resulting in a blank PDF. The base64 file and json are correct.
Thanks for replying

Loren Doornek

  • Gold Member
  • ****
  • Posts: 85
    • View Profile
Re: Transform pdf to base 64
« Reply #3 on: August 24, 2022, 02:04:28 PM »
Try removing all of the line feeds from the Base64 string before adding it to the JSON, so that the base64 data is nothing but one long string of data.  Some newer languages JSON parsers don't really care if the base64 is broken up into short lines.  Since you're sending it via JSON, I'm assuming the receiving end is using some sort of modern parser that may not need the line feeds.

Mike King

  • Diamond Member
  • *****
  • Posts: 3811
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: Transform pdf to base 64
« Reply #4 on: August 24, 2022, 04:02:05 PM »
Assuming you have the PDF file try:

CALL "*tools/readfile","your_file.pdf",fileimage$
base64$=CVS(fileimage$, "ASCII:BASE64",0)

This will put the contents of the PDF file BAS64 encoded into the variable Base64$
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com