Base46 json to pdf


#1

Hi

I have a base64 json file that I need to output in pdf on screen. I’m sure it is simple but I am going round in circles.

Please could somebody just outline the best method to do this as I can’t find anything really relevant on the net.

TIA

Tony


#2

can you elaborate a bit more?

you want to generate a pdf from some json which happens to be encoded in base64?


#3

Hi Zac!

I’m trying to use a service that allows a series of datafields to be sent and merges them into a pdf.

The result is returned in a json object with the actual file in base64. This seems to be a normal way of moving this kind of data.

I need to convert the response field into binary (which is no problem) and then output the whole json file as a pdf - This is where I am having issues. I just keep getting a blank pdf file which implies I am missing the data. I have the json file in a struct with the response field in binary. How do I get it to produce the pdf correctly?

TIA


#4

you’re using lucee to produce html and then converting it to a pdf using cfdocument?


#5

no - I’m using the remote service - there is no HTML involved


#6

is the blank pdf file produced the size you expect?


#7

yes - everything is great!

This produces the pdf

header name="Content-Disposition" value="inline";
content variable = tostring(ready) type="application/pdf";

I’m just not sure of the format required for the variable!


#8

did you try it with just ready, instead of toString(ready)


#9

Still just blank…

Somewhere there is a problem. I have a struct with all the info and the file data in binary.

Can I produce a pdf from that? Or do I have to convert it into something else?


#10

These are the steps I am using…

read file (the return object - myFile)

dataArray = deserializeJSON(myFile);
decode = binaryDecode(dataArray.response, "base64");
structUpdate(dataArray, "response", decode);
ready = serialize(dataArray);
header name="Content-Disposition" value="inline";
content variable = "ready" type="application/pdf";

#11

Try specifying the variable without quotes:

content variable=ready type="application/pdf";

#12

Hi Julian

No that barfed!


#13

OK. That’s what works for me streaming pdf binaries generated using cfdocument, but clearly your binary is different.


#14

Do you know the type of stream that works? I wonder if I need to strip all the json headers from the data first?


#15

Not quite sure what you mean by “stream” I just mean serving the PDF binary over http using cfcontent as you’re apparently doing too.


#16

I have the PDF data as base64 in a json payload.

After conversion the string contains json headers - is there an easy way to remove these from a struct?


#17

I’m not really sure I understand what you’re doing. Why aren’t you just passing the decoded base64 string (i.e. the converted binary) to cfcontent?


#18

Maybe I should - I’ll investigate. Still learning Lucee!


#19

I think I’m on track - Once I get the solution I’ll post it…


#20

I think you want variable=decode.

First, stop saying you want to generate a PDF. You don’t want to do that. :slight_smile: You want to take binary content from a service and display it to the browser. It just so happens the data represents a PDF, but you’re not using lucee to make one.

decode has your binary data in it, so that’s what you want to spray out to the browser. (I’m not sure why you’d want to reserialize after that)

-G