How to open PDF in Delphi’s Webbrowser?

Asked

Viewed 1,920 times

1

In the Webbrowser of Delphi, run a file .html, which is a PDF reader. I need when the reader is opened to 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 am using the pdf.js library: https://mozilla.github.io/pdf.js/

inserir a descrição da imagem aqui

  • Have you tried WebBrowser1.Navigate('PDFJS.getDocument('+QuotedStr(teste.pdf)+')'

  • And where do I step the . html file path? @Andrey

1 answer

0


PDF.js will not open in the protocol file:///, due to security issues with XmlHttpRequest (ajax), the only way I see of making this work is to create a protocol of its own (if possible) in Delphi, or to create a mini HTTP server that only 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 pdf.js html:

ExtractFilePath(Application.ExeName) + 'pdf.html'

Browser other questions tagged

You are not signed in. Login or sign up in order to post.