HTMLViewer.print() and Margins

When in a desktop app, using the WebKit renderer on an HTMLViewer, if I call .print, the margins are set to 1", and I cannot seem to change them.

The OS Printing dialog that comes up does not include a way to change the paper size or margins at all.

I’ve only tried this in OS X so far.

Can I change the margins for the paper to something other than 1"?

You can use PrintToPDFFileMBS, if you do the MBS plugin.

I haven’t tried it myself but have you tried the @page CSS settings?

@Bob Coleman - yes, I did try the @page css settings. They appear to have no effect on Safari at all, and I’ve discovered that WebKit is only available on windows. So on the mac the HTMLViewer is only using the OS-supplied browser, which does not support the @page directives for margin. Chrome and firefox on the mac both do. I assume (though I have not tested) that on windows using webkit it’ll support the @page directives as well.

@Beatrix Willius - I do have the full MBS plugins, but printing to PDF does not solve my need to print out an actual piece of paper… unless I print from the HTMLViewer->PDF, then somehow open the pdf and print that. All programmatically, after the user hits a single “print” button in my app. (i.e., I can’t expect my users to manually go find a PDF that was created from the browser control, open that in whatever viewer they use, then print that. Do you happen to know if there are any other MBS methods for controlling pagesetup for the HTMLViewer control on a Mac? Looking through the API docs, I see some promising things, but Christian’s documentation is… sparse.

Actually, AFAIK, Safari is Webkit, like Chrome. On Windows, native being the Internet Explorer engine, you got to chose one.

You may want to use ScreenRectMBS to capture the HTMLViewer image (drawinto does not work), and print that picture yourself through the regular graphic printing method. That will give you much more control to position and scale it.

[quote=178175:@Michel Bujardet]Actually, AFAIK, Safari is Webkit, like Chrome. On Windows, native being the Internet Explorer engine, you got to chose one.

You may want to use ScreenRectMBS to capture the HTMLViewer image (drawinto does not work), and print that picture yourself through the regular graphic printing method. That will give you much more control to position and scale it.[/quote]

It’s not as simple as “Safari is WebKit”, because Xojo does not actually embed WebKit - as I recall it embeds a somewhat older version of Chromium, and only does so on Windows. Chrome is no longer based on WebKit at all, and has not been for several years.

Interesting idea about the ScreenRectMBS function, but the contents of my HTML viewer are many pages long (I’m generating HTML-based reports and showing them on screen in an HTML viewer. When the user needs to print them is where I’m running into problems getting margins set reasonably.

Does changing the PrinterSetup help?

@shao sean - good thought, but unfortunately there does not appear to be any way to tell the HTMLViewer what printerSetup to use, as you can’t call openPrinter or openPrinterDialog from the HTMLViewer.

Looks like it could be doable through declares… macoslib is missing the NSPrintInfo class, so you would need to create that yourself as well… Depending on how quickly you need this, I can see about tackling it next week…

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/WebKit/Classes/WebFrameView_Class/index.html#//apple_ref/occ/instm/WebFrameView/printOperationWithPrintInfo:

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSPrintInfo_Class/index.html#//apple_ref/doc/uid/TP40004090

@shao sean - thank you! I don’t need it super urgently. If you are seriously willing to take a swing at it, I’d be grateful.

I may take a whack at it as well. I’ll come back and update here if I make any progress on declares myself.

Thanks!

The way to print in Xojo is to use NSPrintOperationMBS.
We have it all there with NSPrintInfoMBS for the margins and passing the WebviewMBS object of the htmlviewer to the print operation.

Thank you @Christian Schmitz! - for anyone in the future looking for a working solution to this, here is my final code. (requires the MBS plugins)

//mainWindow is my main application window //ReportPreviewHTMLViewer is an instance of HTMLViewer that I wish to print. dim pi as new NSPrintInfoMBS() //I arbitrarily chose 20 for the margins. I believe this is in "points" rather than //some page measurement such as mm or in. pi.leftMargin = 20 pi.rightMargin = 20 pi.topMargin = 20 pi.bottomMargin = 20 dim n as NSPrintOperationMBS = NSPrintOperationMBS.printOperationWithView(ReportPreviewHTMLViewer, pi) n.showsPrintPanel = true n.showsProgressPanel = true n.runOperationModalForWindow(mainWindow)

@Kimball and @Christian: ah, good to know.

You only asked about the margins. Of course, there is a method to print the pdf instead of the html. The example is called “Carbon Print” somewhere. I’m using html as a basis, convert it to pdf, add header and page information and print or save the result.