PxPlus User Forum

Main Board => Discussions => Programming => Topic started by: David Reynolds on June 04, 2019, 02:30:39 PM

Title: image/png;base64 string to actual png
Post by: David Reynolds 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
Title: Re: image/png;base64 string to actual png
Post by: Mike King 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)
Title: Re: image/png;base64 string to actual png
Post by: David Reynolds on June 04, 2019, 03:22:24 PM
Worked like a charm.
Thanks Mike!
Title: Re: image/png;base64 string to actual png
Post by: David Reynolds 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
Title: Re: image/png;base64 string to actual png
Post by: Mike King 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.

Title: Re: image/png;base64 string to actual png
Post by: David Reynolds on June 07, 2019, 03:37:55 PM
Nailed it again.
Thanks!