PxPlus User Forum

Twitter Twitter Twitter

Author Topic: image/png;base64 string to actual png  (Read 1761 times)

David Reynolds

  • Member
  • **
  • Posts: 14
    • View Profile
image/png;base64 string to actual png
« on: June 04, 2019, 02:30:39 PM »
I am calling an external API and one of the returned JSON values is a customer signature encoded as
image/png;base64,iVBORw0KGg.......
How do I take that string and create an actual fileName.png (or gif, jpg, bmp, etc)?

Thanks,
David

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: image/png;base64 string to actual png
« Reply #1 on: June 04, 2019, 02:52:45 PM »
Take the data following the comma after 'base64' and pass it through the CVS function.  This will get you file contents which you can write to a png file.

Something like this should work for you: 
(Assumes the "image/png;base64,iv..." is in the variable input$)

Code: [Select]
pngData$=CVS( input$(pos(","=input$)+1), "Base64:ASCII")

open purge (hfn,isz=-1) "somename.png"
write record (lfo) pngData$
close (lfo)
« Last Edit: June 04, 2019, 04:07:47 PM by Mike King »
Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

David Reynolds

  • Member
  • **
  • Posts: 14
    • View Profile
Re: image/png;base64 string to actual png
« Reply #2 on: June 04, 2019, 03:22:24 PM »
Worked like a charm.
Thanks Mike!

David Reynolds

  • Member
  • **
  • Posts: 14
    • View Profile
Re: image/png;base64 string to actual png
« Reply #3 on: June 07, 2019, 03:15:23 PM »
Next thing I need is to print the PNG to a PDF.
We're on PxPlus 2016 on CentOS so my understanding is that there is no direct support for 'PICTURE' with a PNG to PDF (and my tests confirm this).
I tried using the ImageMagick 'convert' utility to change my PNG to JPG but that still doesn't render in the PDF.
Do I have to pass some special options to 'convert' to get a specific type of JPG that's compatible with printing to PDF from Linux?

Thanks again,
David

Mike King

  • Diamond Member
  • *****
  • Posts: 3810
  • Mike King
    • View Profile
    • BBSysco Consulting
Re: image/png;base64 string to actual png
« Reply #4 on: June 07, 2019, 03:22:07 PM »
David,

Make sure the 'HP' parameter is set to 1 in order to use the more advanced HARU PDF libraries.

PxPlus 2019 does support PNG output to PDF files so the simple solution would be to upgrade.  Assuming you are on the maintenance plan (CCP not LAP) you should be able to do this upgrade at no charge.

Mike King
President - BBSysco Consulting
eMail: mike.king@bbsysco.com

David Reynolds

  • Member
  • **
  • Posts: 14
    • View Profile
Re: image/png;base64 string to actual png
« Reply #5 on: June 07, 2019, 03:37:55 PM »
Nailed it again.
Thanks!