Reputation: 2577
I'm trying to decode a base64 string with #ToString( ToBinary( stringToDecode ) )#
and coldfusion gives an error saying the parameter must be a base-64 encoded string. The string comes from a third party, and is supposed to be a pdf file. Here is a part of it:
JVBERi0xLjUKJeLjz9MKMSAwIG9iago8PC9UeXBlL1hPYmplY3QvUmVzb3VyY2VzPDw+Pi9TdWJ0 eXBlL0Zvcm0vQkJveFswIDAgMTUuNDQgMTUuNDZdL01hdHJpeCBbMSAwIDAgMSAwIDBdL0xlbmd0 aCAyMi9Gb3JtVHlwZSAxL0ZpbHRlci9GbGF0ZURlY29kZT4+c3RyZWFtCk9lHxbaEDXZpIO1A/Gj mGuxQdW3qkgKZW5kc3RyZWFtCmVuZG9iagoyIDAgb2JqCjw8L1R5cGUvWE9iamVjdC9SZXNvdXJj ZXM8PD4+L1N1YnR5cGUvRm9ybS9CQm94WzAgMCAxNS40NCAxNS4xOV0vTWF0cml4IFsxIDAgMCAx IDAgMF0vTGVuZ3RoIDIyL0Zvcm1UeXBlIDEvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KtDN8 NkUKYx04Gj8V9LfOboGvN9VRIgplbmRzdHJlYW0KZW5kb2JqCjMgMCBvYmoKPDwvVHlwZS9YT2Jq ZWN0L1Jlc291cmNlczw8Pj4vU3VidHlwZS9Gb3JtL0JCb3hbMCAwIDE1LjQ0IDE1LjcyXS9NYXRy aXggWzEgMCAwIDEgMCAwXS9MZW5ndGggMjIvRm9ybVR5cGUgMS9GaWx0ZXIvRmxhdGVEZWNvZGU+
What I had thought was the spaces were a problem, but I did a replace to replace all " " with "" and they are still in there. Is there something else I should be doing?
Upvotes: 1
Views: 2574
Reputation: 3535
Here's an updated cfscript way of doing it in case anyone is interested:
<cfscript>
// read the binary file and save it as a variable
myPdf = fileReadBinary( expandPath( "/test.pdf" ) );
// encode as base64
myPdf = toBase64( myPdf );
// output the base64 version to the browser
writedump( myPdf );
// convert back to binary
myPdf = toBinary( myPdf );
// write to a new PDF file
fileWrite( expandPath( "/new.pdf" ), myPdf );
</cfscript>
Upvotes: 1
Reputation: 349
In my testing I can take a local pdf and convert it to a string to mimic the string you are getting from the API. I can then take it and convert it back to a PDF.
When I use the string you put above I cannot get it to work. Not sure if something is list from you pasting it here or not.
Here is my code, maybe you can alter it to use the string right from the api.
<cffile action="readbinary" file="#expandPath('./test.pdf')#" variable="thefile"/>
<cfset the_string = toString(theFile)/>
<cfscript>
patrick_string = ToBase64("JVBERi0xLjUKJeLjz9MKMSAwIG9iago8PC9UeXBlL1hPYmplY3QvUmVzb3VyY2VzPDw+Pi9TdWJ0 eXBlL0Zvcm0vQkJveFswIDAgMTUuNDQgMTUuNDZdL01hdHJpeCBbMSAwIDAgMSAwIDBdL0xlbmd0 aCAyMi9Gb3JtVHlwZSAxL0ZpbHRlci9GbGF0ZURlY29kZT4+c3RyZWFtCk9lHxbaEDXZpIO1A/Gj mGuxQdW3qkgKZW5kc3RyZWFtCmVuZG9iagoyIDAgb2JqCjw8L1R5cGUvWE9iamVjdC9SZXNvdXJj ZXM8PD4+L1N1YnR5cGUvRm9ybS9CQm94WzAgMCAxNS40NCAxNS4xOV0vTWF0cml4IFsxIDAgMCAx IDAgMF0vTGVuZ3RoIDIyL0Zvcm1UeXBlIDEvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KtDN8 NkUKYx04Gj8V9LfOboGvN9VRIgplbmRzdHJlYW0KZW5kb2JqCjMgMCBvYmoKPDwvVHlwZS9YT2Jq ZWN0L1Jlc291cmNlczw8Pj4vU3VidHlwZS9Gb3JtL0JCb3hbMCAwIDE1LjQ0IDE1LjcyXS9NYXRy aXggWzEgMCAwIDEgMCAwXS9MZW5ndGggMjIvRm9ybVR5cGUgMS9GaWx0ZXIvRmxhdGVEZWNvZGU+");
string2 = toBinary(ToBase64(the_string));
binencode=BinaryEncode(string2, "Base64");
</cfscript>
<cffile action="write" file="#expandPath('./randy.pdf')#" output="#thefile#" addnewline="No" />
<a href="randy.pdf">click</a>
Updated Code:
<cffile action="readbinary" file="#expandPath('./test.pdf')#" variable="thefile"/>
<cfset the_string = toBase64(theFile)/>
<cfscript>
string2 = toBinary(the_string);
</cfscript>
<cffile action="write" file="#expandPath('./randy.pdf')#" output="#string2#" addnewline="No" />
<a href="randy.pdf">click</a>
Upvotes: 2