How to load a local PDF file into Chromium

1,114 views
Skip to first unread message

Al

unread,
Sep 7, 2011, 9:10:47 PM9/7/11
to delphichromiumembedded
After much frustration, I found a way to do this. In Delphi, create a
simple html file loader, like this:

procedure Form1.LoadPDF(PDFFile, LoadHTM: String; Browser: TChromium);
//PDFFile is the actual full path/filename of the PDF you want to use.
//LoadHTM is the full path/filename of the htm file we will make to
load the PDF,
// I use Documents for the location and Load.htm as the file name.
//GetHTMLColor simply gets your native form color, that TChromium
resides on,
// and converts it to an HTML color tag (when the PDF loads in
Chromium, for
// some unknown reason, it has a small border around the entire
viewer in
// Chromium, so this sets the border to the form color to make it
blend in;
// if anyone knows how to fix this, let me know).

function GetHTMLColor(cl: TColor): string;
var
rgbColor: TColorRef;
begin
rgbColor := ColorToRGB(cl);
Result := 'bgcolor="#' + Format('%.2x%.2x%.2x',
[GetRValue(rgbColor),
GetGValue(rgbColor),
GetBValue(rgbColor)]) + '"';
end;

begin
with TStringList.Create do
begin
Add('<html>');
Add('<head>');
Add('</head>');
Add('<body ' + GetHTMLColor(Form1.Color) + '>');
Add('<embed width="100%" height="98%" name="plugin" src="' +
PDFFile + '" type="application/pdf">');

{height=98% because it puts a vscroll bar on Chromium otherwise}

Add('</body>');
Add('</html>');
SaveToFile(LoadHTM);
Free;
end;
Browser.Load(LoadHTM);
end;

Works great, but make sure your documents are in a good public area
like My Documents/Documents/Public Documents; when I tried it on C:\
the first few times, I could view, but not modify; after moving them
to Documents, I could view and modify the PDF files.

Henri Gourvest

unread,
Sep 8, 2011, 4:39:05 AM9/8/11
to delphichrom...@googlegroups.com
It is probably a problem with the "file" scheme handler provided in samples.
you should comment these lines in the initialization section:
//  CefRegisterCustomScheme('file', True, False, False);
//  CefRegisterSchemeHandlerFactory('file', '', True, TFileScheme);
so it will use the default one provided by chromium itself.

if you prefer to use the file scheme handler provided in demos, get the source from SVN, I have just added the PDF mime type.


2011/9/8 Al <asan...@gmail.com>
After much frustration, I found a way to do this.  In Delphi, create a
simple html file loader
--
Henri Gourvest

Al

unread,
Sep 8, 2011, 10:16:57 AM9/8/11
to delphichromiumembedded
Thanks for the quick response. I am not using the demo code, and
don't have any initialization section in my program at all. I am
using Delphi 7 and I just dropped the TChromium component on the form
and tried to navigate to a local PDF file using:
Chromium1.Load(pdffilenamestring);
Chrome would load to a blank screen, but normal web pages load in
fine. Others have had the same issue. I'll look at this code this
evening and see what it does.

On Sep 8, 1:39 am, Henri Gourvest <hgourv...@gmail.com> wrote:
> It is probably a problem with the "file" scheme handler provided in samples.
> you should comment these lines in the initialization section:
> //  CefRegisterCustomScheme('file', True, False, False);
> //  CefRegisterSchemeHandlerFactory('file', '', True, TFileScheme);
> so it will use the default one provided by chromium itself.
>
> if you prefer to use the file scheme handler provided in demos, get the
> source from SVN, I have just added the PDF mime type.
>
> 2011/9/8 Al <asand...@gmail.com>
Reply all
Reply to author
Forward
0 new messages