0

I have an application that uses a TWebBrowser to display an HTML report. I use a TCanvas, named PrintCanvas to print a page header, footer, and images. Now I'd like to add the content from the TWebBrowser to the canvas so that the HTML report is printed as well. I've tried copying the TWebBrowser content to a Bitmap, and then adding the Bitmap to my PrintCanvas. This works, but only shows the displayed portion of the TWebBrowser.

Is there a way I can copy an entire HTML report to a TCanvas? This would allow me to use my existing printing code. I'm okay with using a method other than the TWebBrowser, if there is a better way.

Another thing I have tried is using the TWebBrowser's ExecWB method to execute the OLECMDID_PRINT command, which opens the browser's print dialog box. This works, but this doesn't allow me to print the other information already on my PrintCanvas, such as the page header, footer, and images.

Kyle Williamson
  • 2,251
  • 6
  • 43
  • 75
  • You can use html component instead of canvas – Bosshoss Jan 13 '23 at 22:10
  • 1
    You cannot simply copy HTML to a TCanvas. HTML is a kind of page description language while TCanvas is a drawing surface. You have to translate HTML to the drawing instructions a TCanvas can do. This is not an easy task. It is probably easier to add to the HTML report the required HTML parts to add a page header, footer and images and then use TWebBrowser print facility to have the whole printed. And by the way, forget about TWebBrowser and replace it by TEdgeBrowser (TWebBrowser is based on Internet Explorer now deprecated and TEdgeBrowser is based on Edge which is the last web browser). – fpiette Jan 14 '23 at 08:05
  • 1
    Continuation of my previous comment: HTML is text based so you can open it with Delphi as any other text file. You can the modify this text file to add the HTML instructions to add a page header, footer and images. Then save the file and load it in TEdgeBrowser to render it on screen and print it. – fpiette Jan 14 '23 at 08:07

1 Answers1

2

There are several ways to do that but all of them require different components.

Solution 1: Since you mention that you're OK using an alternative component then I would use CEF4Delphi.

CEF has an offscreen rendering mode (OSR) that draws the web contents in a raw bitmap buffer.

Several CEF4Delphi demos show how to use that mode like SimpleOSRBrowser, TabbedOSRBrowser or KioskOSRBrowser. You can use any demo as a template for your application.

Those demos copy the raw bitmap data with the web contents from the "buffer" parameter in the TChromiumCore.OnPaint event. As you can see here, the SimpleOSRBrowser copies scanlines from the buffer parameter to a custom bitmap.

Alternatively you can also use a CEF browser without using the offscreen rendering mode. If you build the MiniBrowser demo you can try the "Take screenshot" option in the menu button at the top-right corner. This option uses the "Page.captureScreenshot" DevTools method to take the screenshot and it saves it as a PNG image here.

Solution 2: Use WebView4Delphi. WebView4Delphi also has a MiniBrowser demo with a "Take snapshot..." menu option that calls TWVBrowser.CapturePreview to save the PNG file but you can also use the DevTools method mentioned previously with WebView4Delphi if you call TWVBrowser.CallDevToolsProtocolMethod.