如何在rst2pdf中设置rightside容器为页眉顶部对齐
问题背景
我有一段用于页面页眉的reStructuredText代码,期望左侧显示Logo图片,右侧文档标题右对齐且位于页眉顶部或中部。当前文档标题在图片底部、页面中间左对齐。我的样式文件目前仅配置了image的alignment为TA_LEFT,通过Python脚本调用rst2pdf生成PDF。已发现可在样式中使用rightside类设置alignment: TA_RIGHT实现右对齐,但尝试align、valign等属性设置垂直顶部对齐无效,请问如何在样式文件中配置rightside容器使其位于页眉顶部?
相关代码
页眉reStructuredText代码
.. header:: .. container:: twocol .. container:: leftside .. image:: Logo.png :height: 152 :width: 474 .. container:: rightside Document Title
样式文件(simple.style)
styles: image: alignment: TA_LEFT
Python调用代码
rst2pdf.createpdf.main(["-v", "--stylesheets=simple.style", f"{out_file}2.rst", f"{out_file}2.pdf"])
解决方案
要实现右侧标题顶部右对齐,需修改样式文件,将twocol容器设置为表格布局,同时为左右子容器配置垂直对齐规则:
styles: image: alignment: TA_LEFT twocol: display: table width: 100% # 占满页眉宽度 table-layout: fixed leftside: display: table-cell valign: TOP # 垂直顶部对齐 rightside: display: table-cell alignment: TA_RIGHT # 水平右对齐 valign: TOP # 垂直顶部对齐 padding-top: 0 # 移除默认顶部内边距,确保紧贴页眉顶部
原理说明
- 将
twocol容器设为table布局,让左右子容器以表格单元格形式布局,支持垂直对齐属性 valign: TOP强制单元格内容垂直居顶,解决标题默认跟图片底部对齐的问题width: 100%确保页眉容器占满页面宽度,让右侧标题能完全靠右对齐
内容的提问来源于stack exchange,提问作者Superdooperhero
相关产品推荐
相关产品推荐

