How to open PDF in Delphi WebBrowser?

1

Delphi WebBrowser , I run a .html file, which is a PDF reader. I need when I open the reader, load a PDF stored in a Delphi string variable.

procedure TForm2.Button1Click(Sender: TObject);
var arquivo : string;
begin
 arquivo := 'teste.pdf';
 WebBrowser1.Navigate('C:\zLocal2016\src_Teste\PDF_atual\pdf\web\viewer.html');
end;

Note: I'm using the pdf.js library: link

    
asked by anonymous 27.09.2017 / 16:31

1 answer

1

PDF.js will not open in the file:/// protocol, this is because of security issues with XmlHttpRequest (ajax), the only way I can see to make this work is to create a proper protocol (if possible ) in Delphi, or else create a mini HTTP server that just for the use of PDF.js

A simple example of an HTTP server would be this:

procedure TMainForm.HTTPServerCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
begin
  AResponseInfo.ContentType := 'text/html';
  AResponseInfo.CharSet := 'utf-8';
  AResponseInfo.ContentStream := TMemoryStream.Create;

  TMemoryStream(AResponseInfo.ContentStream).LoadFromFile(ExtractFilePath(Application.ExeName) + 'pdf.html');
end;

The command contains the pdf.js html:

ExtractFilePath(Application.ExeName) + 'pdf.html'
    
30.09.2017 / 03:50