Jump to content
msd

Encode (pdf, jpg, ...) in to XML

Recommended Posts

Hello,


We need to create an XML document file for an online document exchange system, and that XML file must include attachments.

I can show you how it appears, but I'm not sure how to add/encode documents into a string that will be used to represent documents in an XML file (like attachment).

In other words, how to encode a file into a string that may be used to represent it in an XML structure.


Thank you very much...

Share this post


Link to post

Are you writing both ends of the document exchange system? If you must access an existing system, then you must get the specification of the attachment.

  • Thanks 2

Share this post


Link to post

Hello, 

 

This is an example from an existing document...

 

<cbc:EmbeddedDocumentBinaryObject mimeCode="application/pdf" filename="document_name.pdf">JVBERi0xLjcNCiW.....EmbeddedDocumentBinaryObject>

 

This is a universal app, so there is no specific need for some encoding or decoding, just a simple representation of the binary representation of the document...

Share this post


Link to post

I wrote that this is a universal doc exchange web app. I'm from the client side. I need to push XML to the web app in XML format with an embedded PDF document.

Share this post


Link to post

I'm probably stupid, but you still don't clearly tell if you write the server side.

If you write the server side and the client side, you can define things like you prefer. For example encoding the PDF file as a base64 stream which is just text and put it in the XML document.

If you don't write the server side, then contact the author of the server to get the requirements he selected.

Share this post


Link to post

Because I only work on the client side, I have no experience with server-side encoding.

I'm aware that the documentation specifies that nodes with attachments must be document/pdf in binary format.

My initial question was about how to convert PDF files to binary and then put it into XML.

I'll try Base64 Encoding first, and if it works, I'll put a bit of code here for the other members to see.

 

Thank you for your suggestions...

  • Like 1

Share this post


Link to post
5 hours ago, msd said:

I'm aware that the documentation specifies that nodes with attachments must be document/pdf in binary format.

 

I am pretty sure it can't be (RAW) binary (will most certainly contain characters that are illegal in XML file) . Most likely Base64 encoded. But should check from Server side developers/docs

Share this post


Link to post

I'm aware that the documentation specifies that nodes with attachments must be document/pdf in binary format.

 

Would you reproduce that part of the documentation here? I ask because what you tell is largely incomplete and confusing.

 

If you show us a complete XML document with a PDF file attached, that could help. Someone could be able to recognize the encoding format. Maybe me.

  • Like 1

Share this post


Link to post

This is a link: https://base64.guru/converter/encode/pdf where you can find how to encode PDF and the correct way to add it to XML.

 

This link was given to us by the developer, and when we use this online tool and generate an XML tag, everything works fine:-)

 

It looks like a simple Base64 encode from Delphi is OK, so if you can give me a sample (only encode/decode part of code).

Share this post


Link to post

I was made function that works the job 🙂 if someone need it...

function EncodePDF(const AFileName: string): string;
var
  inStream: TStream;
  outStream: TStringStream;
begin
  inStream := TFileStream.Create(AFileName, fmOpenRead);
  try
    outStream := TStringStream.Create;
    try
      TNetEncoding.Base64.Encode(inStream, outStream);
    finally
      Result := outStream.DataString;
      outStream.Free;
    end;
  finally
    inStream.Free;
  end;
end;

if someone has better idea, please post it here 😉

Share this post


Link to post

The try/finally coding in the above is incorrect. It should be:

 

function EncodePDF(const AFileName: string): string;
var
  inStream: TStream;
  outStream: TStringStream;
begin
  inStream := TFileStream.Create(AFileName, fmOpenRead);
  try
    outStream := TStringStream.Create;
    try
      TNetEncoding.Base64.Encode(inStream, outStream);
      Result := outStream.DataString; 
    finally
      outStream.Free;
    end;
  finally
    inStream.Free;
  end;
end;
  • Thanks 1

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×