TTMSFNCWXPDFViewer LoadFromBase64 procedure not working

Hey guys, I'm attaching a small program where I try to convert a PDF file to Base64 and then load it into the TTMSFNCWXPDFViewer with LoadFromBase64, but the PDF doesn't display. You can download the test PDF here: https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf . To test the generated Base64, you can use this URL:
https://base64.guru/converter/decode/pdf
TESTFNC.zip (106.1 KB)

Hi,

TNetEncoding.Base64.Encode adds linebreaks in the base64 encoded string making it invalid. You can use TBase64Encoding instead:

procedure LoadPDFAsBase64AndShow(const PDFPath, TxtOutputPath: string; Viewer: TTMSFNCWXPDFViewer);
var
  FileStream: TFileStream;
  StringStream: TStringStream;
  Base64Str: string;
  base64: TBase64Encoding;
begin
  if not FileExists(PDFPath) then
    raise Exception.Create('Archivo PDF no encontrado: ' + PDFPath);

  FileStream := TFileStream.Create(PDFPath, fmOpenRead or fmShareDenyWrite);
  base64 := TBase64Encoding.Create(0);
  try
    StringStream := TStringStream.Create('', TEncoding.ASCII);
    try
      // Codifica a Base64
      base64.Encode(FileStream, StringStream);
      Base64Str := StringStream.DataString;

      // Guarda el Base64 en archivo
      StringStream.SaveToFile(TxtOutputPath);

      // Carga el PDF codificado en Base64 al visor
      Viewer.LoadFromBase64(Base64Str);
    finally
      StringStream.Free;
    end;
  finally
    FileStream.Free;
    base64.Free;
  end;
end;

Thanks Tünde, your function works perfectly.

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.