D.Tkalcec (RTC)
|
|
« Reply #3 on: May 26, 2022, 09:37:48 PM » |
|
First of all, you should NOT use a TStringStream when working with binary data in Delphi versions that have native Unicode support, because that can already result in your binary data to get corrupted, due to implicit String type conversions that can be injected by the Delphi compiler. To avoid these unwanted conversions, you need to work with data types that store your binary data as raw bytes and NOT as Unicode characters.
If you are familiar with the TMemoryStream class, you could use it instead of TStringStream for your PDFStream object to store your PDF document in memory without having to worry about data corruption, then you could copy that data into an array of bytes when you want to pass it on to the Mine_EncodeEx function for encoding, or ... if you want a simpler alternative ... you could use the "TRtcByteArrayStream" class from the "rtcSystem.pas" unit, which implements all the necessary methods of a TStream class, but uses an array of bytes internally for data storage, and provides you with a simple read-only "GetBytes" property to access that data directly, as an array of bytes.
In either case, once you have that data in an array of bytes and have passed it on to the Mime_EncodeEx function, you will get another array of bytes back, which should contain that same data in a Base-64 encoding. If you wish to convert that Base-64 encoded binary data into a Delphi String, you can use the RtcBytesToString function from the "rtcSystem.pas" unit.
Long story, short ... if you change your code to make your "PDFStream" into a "TRtcByteArrayStream" instead of a "TStringStream", you should be able to do this ...
js.S['pdfdata'] := RtcBytesToString(Mime_EncodeEx(PDFStream.GetBytes));
... without having to worry about data corruption happening while reading or writing your stream, and ... unless something else is going on here ... without seeing the "Range check Error" popping up while running your code with the Debugger in Delphi.
|