如何设置QTextDocument边距和其他属性(setHTML,print to pdf)? [英] How to set QTextDocument margins and other properties (setHTML, print to pdf)?

查看:1942
本文介绍了如何设置QTextDocument边距和其他属性(setHTML,print to pdf)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的证书类来生成一些图片和数据中的pdf文档。设置图像源后,我调用 generate()函数并获取test.pdf输出文件。该文档基于 QTextDocument 类使用 setHtml(html)方法创建。



问题在于我在文档周围有巨大的空白区域,而我希望带有徽标图像的标题REPORT位于页面的顶部。我也想在表中添加较低的边框,但据我所知它不支持Qt(支持的HTML子集)。



Python3代码:

 class证书:

def __init __(self):
self.logo = None
pdffile ='test.pdf'
self.histogram = None
self.printer = QPrinter()
self.printer.setPageSize(QPrinter.Letter)
self.printer.setOutputFormat(QPrinter.PdfFormat)
self。 printer.setOutputFileName(pdffile)

def generate(self):
document = QTextDocument()
html =
html + =('< head> < title>报告< / title>< style>< / style>< / head>'
'< body>< table width =100%>< tr>'
'< td>< img src ={}width =30>< / td>'
'< td> ;< h1> REPORT< / h1>< / td>'
'< / tr>< / table>'
'< p align = right>< img src = {}width =300>< / p>'
'< p align = right>示例< / p>< / body>').format(self.logo,self.histogram )
document.setHtml(html)
document.print_(self.printer)

我以前从未广泛使用过html,也从来没有使用过QTextDocument,并希望得到关于如何控制文档边距和表格属性的任何建议。



我想要的其他相关属性控制是分辨率 - 我使用像素图像大小,并需要知道页面和边界大小像素。



编辑:问题几乎回答了@mata。我现在可以设置任何边距和分辨率,但不知道如何控制图像和字体大小。例如。如果我需要一个图像总是50毫米宽,HTML标题和主要文本字体大小在视觉上是相同的 - 如何实现它?


$ b EDITED2:最后一部分也解决了。这是由@mata修改后的代码,它为任何 dpi 值提供了相同的结果:

  dpi = 96 

document = QTextDocument()
html =
< head>
< title>报告< / title> ;
< style>
< / style>
< / head>
< body>
< table width =100%>
< tr>
< td>< img src ={0}width ={1}>< / td>
< td>< h1> REPORT< / h1>< / td>
< / tr>
< / table>
< hr>
< p align = right><< ;< / p>
< / body> $ b< $ b.format('D:\Documents\IST Projects\diashape\docbook\Installation\images\istlogo_medium.png',
40 * dpi / 96,
'D:\ Documents \IST Projects \diashape\docbook\Installati on\images\istlogo_medium.png',
200 * dpi / 96)

document.setHtml(html)

printer = QPrinter()
font = QFont()
font.setPointSize(12 * dpi / 96)
document.setDefaultFont(font)
printer.setResolution(dpi)
...


解决方案

您可以在构造函数中指定要使用的分辨率您创建QPrinter。
然后,在设置页面大小后,可以使用 width height 和<$ c $打印机上的c> resolution 来找出这些值,这里是我得到的信 (dpi值可以不同,它们取决于屏幕或打印机):

  QPrinter(QPrinter.ScreenResolution)#96dpi,752x992 
QPrinter(QPrinter.PrinterResolution)#72dpi,564x744
QPrinter(QPrinter.HighResolution)#1200dpi,9400x12400

您也可以直接使用 setResolution
由width和height返回的大小是页面大小(与pageRect()。size()相同),与纸张大小不同 - 因为页面也有边距,您可以像这样设置边距:

  printer.setPageMargins(12,16,12,20,QPrinter.Millimeter)

这会将左右边距设置为12毫米,顶部为16毫米,底部为20毫米 - 例如,如果您需要较少的空白空间,您可以明显只需使用较小的值。
您应该将文档大小设置为生成大小的大小:

  document.setPageSize(QSizeF(printer .pageRect()。size()))

你已经注意到了,html的子集和允许的CSS是非常有限的,特别是格式化表。但不是在桌子上使用较低的边框,您可以使用小时,这可能看起来像你想要的。
至少它看起来并不坏,如果我测试它是这样的:

 来自PyQt4.QtGui import * 
from PyQt4.QtCore import *

a = QApplication([])
document = QTextDocument()
html =
< head>
< title>报告< / title>
<样式>
< / style>
< / head>
< body>
< table width =100%>
< tr>
< td>< img src ={}width =30>< / td>
< td>< h1> REPORT< / h1>< / td>
< / tr>
< / table>
< hr>
< p align = right>< img src ={}width =300>< / p>
< p align = right>示例< / p>
< ; / body>
.format('','')

document.setHtml(html)

printer = QPrinter()
printer.setResolution(96)
printer.setPageSize(QPrinter.Letter)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName(test.pdf)
printer.setPageMargins(12,16,12,20,QPrinter.Millimeter)
document.setPageSize(QSizeF(printer.pageRect()。size() ))
print(document.pageSize(),printer.resolution(),printer.pageRect())

document.print_(printer)


I have the following certificate class for producing pdf document out of some images and data. After setting image sources, I call generate() function and get test.pdf output file. The document is created based on QTextDocument class using setHtml(html) method.

The problem is that I have huge white spaces around the document, while I want the title 'REPORT' with logo image to be on the very top of the page. I would also like to add lower border to the table, but as I understand it is not supported by Qt (Supported HTML Subset).

Python3 code:

class certificate:

def __init__(self):
    self.logo = None
    pdffile = 'test.pdf'
    self.histogram = None
    self.printer = QPrinter()
    self.printer.setPageSize(QPrinter.Letter)
    self.printer.setOutputFormat(QPrinter.PdfFormat)
    self.printer.setOutputFileName(pdffile)

def generate(self):
    document = QTextDocument()
    html = ""
    html += ('<head><title>Report</title><style></style></head>'
                 '<body><table width="100%"><tr>'
                    '<td><img src="{}" width="30"></td>'
                    '<td><h1>REPORT</h1></td>'
                 '</tr></table>'
                 '<p align=right><img src="{}" width="300"></p>'
                 '<p align=right>Sample</p></body>').format(self.logo, self.histogram)
    document.setHtml(html)
    document.print_(self.printer)

I never extensively used html before and never worked with QTextDocument, and would appreciate any advice on how to control document margins and table properties.

Other related property I want to control is resolution - I use pixel image size and need to know page and margin sizes in pixels.

EDITED: The question is almost answered by @mata. I can set now any margins and resolution, but do not understand how to control image and font sizes. E.g. if I need that an image is always 50mm wide, and html header and main text font sizes are visually the same - how to implement it?

EDITED2: The last part is solved too. Here is modified code by @mata, it gives the same result for any dpi value:

dpi=96

document = QTextDocument()
html = """
<head>
    <title>Report</title>
    <style>
    </style>
</head>
<body>
    <table width="100%">
        <tr>
            <td><img src="{0}" width="{1}"></td>
            <td><h1>REPORT</h1></td>
        </tr>
    </table>
    <hr>
    <p align=right><img src="{2}" width="{3}"></p>
    <p align=right>Sample</p>
</body>
""".format('D:\Documents\IST Projects\diashape\docbook\Installation\images\istlogo_medium.png',
              40*dpi/96, 
              'D:\Documents\IST Projects\diashape\docbook\Installation\images\istlogo_medium.png', 
              200*dpi/96)

document.setHtml(html)

printer = QPrinter()
font = QFont()
font.setPointSize(12*dpi/96)
document.setDefaultFont(font)
printer.setResolution(dpi)
...

解决方案

You can specify what resolution you want to use in the constructor when you create the QPrinter. Then after you've set the pagesize, you can use width, height and resolution on the printer to fint out that values, here's what I got for Letter (dpi-values can be different, they depend on the screen or the printer):

QPrinter(QPrinter.ScreenResolution)   #   96dpi,  752x992
QPrinter(QPrinter.PrinterResolution)  #   72dpi,  564x744
QPrinter(QPrinter.HighResolution)     # 1200dpi, 9400x12400

You can also set the dpi directly using setResolution. The size returned by width and height is the page size (same as pageRect().size()), which ist not the same as the paper size - as the page also has margins, which you can set like this:

printer.setPageMargins(12, 16, 12, 20, QPrinter.Millimeter)

this sets left and right margins to 12mm, top to 16mm and bottom to 20mm - just for example, if you want less white space you can obviously just use smaller values. And you should set the document size to the size of the resulting size:

document.setPageSize(QSizeF(printer.pageRect().size()))

as you've noticed yourself, the subset of html and css allowed is very limited, specially for formatting tables. But instead of using a lower border on the table you could just use a hr, which probably will look like you want it. At least it doesn't look that bad if I test it like this:

from PyQt4.QtGui import *
from PyQt4.QtCore import *

a=QApplication([])
document = QTextDocument()
html = """
<head>
    <title>Report</title>
    <style>
    </style>
</head>
<body>
    <table width="100%">
        <tr>
            <td><img src="{}" width="30"></td>
            <td><h1>REPORT</h1></td>
        </tr>
    </table>
    <hr>
    <p align=right><img src="{}" width="300"></p>
    <p align=right>Sample</p>
</body>
""".format('', '')

document.setHtml(html)

printer = QPrinter()
printer.setResolution(96)
printer.setPageSize(QPrinter.Letter)
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("test.pdf")
printer.setPageMargins(12, 16, 12, 20, QPrinter.Millimeter)
document.setPageSize(QSizeF(printer.pageRect().size()))
print(document.pageSize(), printer.resolution(), printer.pageRect())

document.print_(printer)

这篇关于如何设置QTextDocument边距和其他属性(setHTML,print to pdf)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆