ThatUser
ThatUser

Reputation: 517

Decoded Base64 string from RoyalMail not loading as PDF

Creating an application that talks to RoyalMail to get a label code and tries to print off the label. In their docs a 'printLabelResponse' contains a PDF in a Base64 string, they gave an example:

JVBERi0xLjYKJeTjz9IKMSAwIG9iagpbL1BERi9JbWFnZUIvSW1hZ2VDL0ltYWdlSS9UZXh0XQpl
bmRvYmoKNCAwIG9iago8PC9MZW5ndGggNSAwIFIKL0ZpbHRlci9GbGF0ZURlY29kZQo+PgpzdHJl
YW0KeJwDAAAAAAEKZW5kc3RyZWFtCmVuZG9iago1IDAgb2JqCjgKZW5kb2JqCjYgMCBvYmoKPDwv
+fr/xAAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgv/xAC1EQACAQIEBAMEBwUEBAABAncA
AQIDEQQFITEGEkFRB2FxEyIygQgUQpGhscEJIzNS8BVictEKFiQ04SXxFxgZGiYnKCkqNTY3ODk6
Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqCg4SFhoeIiYqSk5SVlpeYmZqio6Slpqeo
MCBSCi9JbmZvIDMyIDAgUgovSURbPDZDM0VCNEREOEE2OTNEMTVDQUE4NkRCODJCNTc2MTIzPjw2
QzNFQjRERDhBNjkzRDE1Q0FBODZEQjgyQjU3NjEyMz5dCj4+CnN0YXJ0eHJlZgoxMzI1OTYKJSVF
T0YK

The code below is what i use to then create the PDF file (base64Text is the Base64 encoded string from above)

string filePath = Directory.GetCurrentDirectory() + @"\output_PDF.pdf";
byte[] base64TextBytes = Convert.FromBase64String(base64Text);
File.WriteAllBytes(filePath, base64TextBytes);

I have converted other PDF files into Base64 strings and saved them into a text file, then copy-pasted that text into the soap/xml file in the 'label' tag that im reading locally to simulate the message from Royal Mail and managed to save this back as a PDF where it opened successfully.

The Output from my code from the base64 string above is:

%PDF-1.6
%����
1 0 obj
[/PDF/ImageB/ImageC/ImageI/Text]
endobj
4 0 obj
<</Length 5 0 R
/Filter/FlateDecode
>>
stream
x�    
 endstream
endobj
5 0 obj
8
endobj
6 0 obj
<</Length 7 0 R
/Filter/FlateDecode
>>
stream
x�3P0P0��P016R032219 00000 n 
0000032743 00000 n 
0000103489 00000 n 
0000103467 00000 n 
0000105070 00000 n 
0000104685 00000 n 
0000105419 00000 n 
trailer
<</Size 47
/Root 46 0 R
/Info 36 0 R
/ID[<3EE4406240B0CAF52A875D8627BD8575><3EE4406240B0CAF52A875D8627BD8575>]
>>
startxref
105491
%%EOF

However this wont open in Adobe Acrobat reading and it wont print.

Is this Base64 string incorrect or am i reading it incorrectly, or something else? Many Thanks

Upvotes: 2

Views: 499

Answers (1)

Random Man Wangler
Random Man Wangler

Reputation: 334

I tried converting it using a process I know works and the file couldn't be opened in adobe reader. I used a different encoded string found elsewhere and it worked. The base64 string you have in your question is malformed.

Upvotes: 1

Related Questions