从IE / Delphi打印带有忠实CSS格式的HTML?

时间:2022-09-04 21:10:30

I've got a small program that displays third-party generated HTML pages. It's really just a wrapper around a locked-down TWebBrowser component (which is just an IE wrapper, I believe).

我有一个显示第三方生成的HTML页面的小程序。它实际上只是一个锁定的TWebBrowser组件的包装器(我相信它只是一个IE包装器)。

The client wants to print these pages now too - but the TWebBrowser print method doesn't render all of the CSS. For instance, where the client uses blocks of background colour, I just get the background colour. You can actually see this on some websites when you get IE to print the pages and some of the CSS-type formatting disappears.

客户端现在也想打印这些页面 - 但TWebBrowser打印方法不会渲染所有CSS。例如,客户端使用背景颜色块时,我只获得背景颜色。当你让IE打印页面并且一些CSS类型的格式消失时,你可以在某些网站上看到这个。

I originally convinced myself (stupidly) that the problem was because the CSS was an external reference, but after I'd made the CSS internal to the .HTML file the problem remained.

我最初确信自己(愚蠢地)认为问题是因为CSS是一个外部引用,但在我将CSS内部放到.HTML文件之后问题仍然存在。

Is there a component or technique I can use to get printed output that includes this CSS formatting? I wondered about trying to turn the browser page into a PDF first (that would have other advantages for me too, later) but I don't see any obvious way of doing that either.

是否有可用于获取包含此CSS格式的打印输出的组件或技术?我想知道试图将浏览器页面首先变成PDF(这对我来说也会有其他优点),但我也没有看到任何明显的方法。

I guess really I'm saying - printed output from IE sucks, is there a way of getting a better approximation of what you see on-screen? :-)

我想我真的在说 - 来自IE的打印输出糟透了,有没有办法更好地逼近你在屏幕上看到的内容? :-)

edit: Okay, moved this on now. I can persuade the printed output to include the CSS colour elements, thanks to Will and Onnodb's suggestions - my sample page prints okay, and given that the user understands the significance of the registry setting approach, he's happy to go that way with it (it's going to be used on at most a half-dozen desktops in a locked-down corporate-type office).

编辑:好的,现在就移动它。我可以说服打印输出包含CSS颜色元素,感谢Will和Onnodb的建议 - 我的样本页面打印好了,并且鉴于用户理解注册表设置方法的重要性,他很高兴用它去那样(它是将在一个锁定的公司型办公室中使用最多六个桌面)。

My problem now (which might itself become a new S/O post) is that there's something in their HTML/CSS that is killing IE. Firefox is happy to print it all out just fine (and so does Safari), but IE 7 and 8 will both show a print-preview of the pages but die during 'real' printing.

我现在的问题(可能本身就是一个新的S / O帖子)是他们的HTML / CSS中有一些东西正在杀死IE。 Firefox很乐意将它全部打印出来(Safari也是如此),但IE 7和8都会显示页面的打印预览,但在“真实”打印过程中会死掉。

Given that replacing TWebBrowser with some kind of TGeckoBrowser(!) is probably more trouble than it's worth (and might not fly with the client), I'll have to try and figure out what bit of the (nasty, convoluted) formatting code is causing the problem. There are a multitude of DIVs in there with a mixture of relative and absolute positions... :-)

鉴于将TWebBrowser替换为某种类型的TGeckoBrowser(!)可能比它的价值更麻烦(并且可能不会与客户端一起飞行),我将不得不尝试找出(令人讨厌的,错综复杂的)格式化代码的位置。造成这个问题。那里有很多DIV,有相对和绝对位置...... :-)

2 个解决方案

#1


5  

Are you linking the stylesheets with media="screen,print" in their link/style tags? Also with printing you need to bear in mind things like their printer settings: does a user want to let you print out page background colours? Have you defined the font face specifically or has the user set a preferred printing font of their own in the browser properties?

您是否在其链接/样式标签中将样式表与media =“screen,print”相关联?另外,在打印时,您需要记住打印机设置等内容:用户是否希望打印出页面背景颜色?您是否专门定义了字体或者用户是否在浏览器属性中设置了自己的首选打印字体?

It's generally a good idea to write a totally separate stylesheet for printing purposes, since serif fonts like Georgia are easier to read on paper, and the font size may need changing. Images might want to be hidden or scaled down too.

为了打印目的,编写完全独立的样式表通常是个好主意,因为像Georgia这样的衬线字体更容易在纸上阅读,字体大小可能需要更改。图像可能也希望隐藏或缩小。

#2


3  

AFAIK, IE has a setting on its "print settings" dialog that allows the user to choose whether she wants to print background colours or not. You'd have to change that setting, either by letting the user do so herself, or by changing the registry programmatically. Either way, there doesn't seem to be a way to change this setting on a per-document basis.

AFAIK,IE在其“打印设置”对话框中有一个设置,允许用户选择是否要打印背景颜色。您必须更改该设置,方法是让用户自己这样做,或者通过编程方式更改注册表。无论哪种方式,似乎都没有办法在每个文档的基础上更改此设置。

#1


5  

Are you linking the stylesheets with media="screen,print" in their link/style tags? Also with printing you need to bear in mind things like their printer settings: does a user want to let you print out page background colours? Have you defined the font face specifically or has the user set a preferred printing font of their own in the browser properties?

您是否在其链接/样式标签中将样式表与media =“screen,print”相关联?另外,在打印时,您需要记住打印机设置等内容:用户是否希望打印出页面背景颜色?您是否专门定义了字体或者用户是否在浏览器属性中设置了自己的首选打印字体?

It's generally a good idea to write a totally separate stylesheet for printing purposes, since serif fonts like Georgia are easier to read on paper, and the font size may need changing. Images might want to be hidden or scaled down too.

为了打印目的,编写完全独立的样式表通常是个好主意,因为像Georgia这样的衬线字体更容易在纸上阅读,字体大小可能需要更改。图像可能也希望隐藏或缩小。

#2


3  

AFAIK, IE has a setting on its "print settings" dialog that allows the user to choose whether she wants to print background colours or not. You'd have to change that setting, either by letting the user do so herself, or by changing the registry programmatically. Either way, there doesn't seem to be a way to change this setting on a per-document basis.

AFAIK,IE在其“打印设置”对话框中有一个设置,允许用户选择是否要打印背景颜色。您必须更改该设置,方法是让用户自己这样做,或者通过编程方式更改注册表。无论哪种方式,似乎都没有办法在每个文档的基础上更改此设置。