【发布时间】:2019-09-06 11:36:57
【问题描述】:
我想使用 python 创建一个用于打印的多页 PDF 文件。我更喜欢使用可以在任何环境中正常工作的库来执行此操作,因此我尝试使用 matplotlib。
我可以制作所需的A4大小的图形,并创建一个多页的PDF文件,但是图形艺术家缩小而不占整个页面。
我可以生成 A4 大小页面的 PDF 并在图形周围填充,或者生成页面尺寸较小的 PDF 文件(页面在图形周围缩小以移除填充,图形保持相同大小)。
有人可以帮忙吗?
(我不认为这是重复的,关于填充还有其他问题,但与我的目标不同,我看不到任何对我有用的答案 - 我已经搜索了好几天!)
到目前为止,我在几个 SO 问题/答案之后尝试过的是:
- 在
pyplot.savefig()函数中使用bbox_inches='tight', pad_inches=0,可提供小于A4 且无内边距的PDF 页面。
(奇怪的是,这适用于 EPS 格式,它可以在 A4 纸上直接打印成实际尺寸,但我不知道如何制作多页文件。)
如果我不使用
bbox_inches='tight', pad_inches=0,图形大小看起来或多或少相同,但周围的填充填充空间以制作 A4 页面。使用
fig.tight_layout(rect=[0,0,1,1])或fig.tight_layout(pad=0),保留填充。
此示例代码以 EPS(A4 无内边距)、PDF(A4 内边距)和 TIFF(无内边距但尺寸较小,因此打印时有内边距)生成图形:
import matplotlib.pyplot as plt
from matplotlib import gridspec
def plot_form_page():
# A4 canvas
fig_width_cm = 21 # A4 page
fig_height_cm = 29.7
inches_per_cm = 1 / 2.54 # Convert cm to inches
fig_width = fig_width_cm * inches_per_cm # width in inches
fig_height = fig_height_cm * inches_per_cm # height in inches
fig_size = [fig_width, fig_height]
plt.rc('text', usetex=False) # so that LaTeX is not needed when creating a PDF with PdfPages later on
fig = plt.figure()
fig.set_size_inches(fig_size)
fig.set_facecolor('#9999ff')
gs = gridspec.GridSpec(29, 21, wspace=0.1, hspace=0.1)
# external axis
ax0 = fig.add_subplot(gs[:, :])
# for side in ["top", "bottom", "left", "right"]:
# ax0.spines[side].set_visible(False)
# header
ax1 = fig.add_subplot(gs[1:4, 1:4])
ax1.set_facecolor('#bbffbb')
ax2 = fig.add_subplot(gs[1:4, 4:-6])
ax2.set_facecolor('#ffbbff')
ax3 = fig.add_subplot(gs[1:4, -6:-1])
ax3.set_facecolor('#00bbff')
# form
ax4, ax5, ax6, ax7 = [fig.add_subplot(gs[ 5+(i*6):10+(i*6), 1:10]) for i in range(4)]
[ax8, ax9, ax10, ax11] = [fig.add_subplot(gs[ 5+(i*6):10+(i*6), 11:20]) for i in range(4)]
axes = [ax4, ax8, ax5, ax9, ax6, ax10, ax7, ax11]
for ax in axes:
ax.set_facecolor('#bbbbbb')
for ax in fig.axes:
ax.set_xticks([])
ax.set_yticks([])
print(fig.properties()['figwidth'], fig.properties()['figheight'])
# from previous tests:
#fig.tight_layout(pad=0)
plot_form_page()
plt.savefig('page.tiff',
dpi=300,
orientation='portrait',
box_inches='tight', pad_inches=0)
plt.savefig('page.pdf',
dpi=300,
orientation='portrait',
bbox_inches='tight', pad_inches=0)
plt.savefig('page.eps',
dpi=300,
orientation='portrait',
papertype='a4',
bbox_inches='tight', pad_inches=0)
fig.properties() 给我正确的 A4 尺寸(英寸):
fig.properties()['figwidth']
fig.properties()['figheight']
8.26771653543307 11.69291338582677
但在打印时,图形会缩小以允许填充
您可以在此处查看 PDF 文件中的效果:
https://www.dropbox.com/s/naqy9vj4ht6g2mv/Screenshot%202019-04-16%20at%2011.46.48.png?dl=0
有了 EPS 就不是什么问题了:
https://www.dropbox.com/s/umycd8ofwod3nk5/Screenshot%202019-04-16%20at%2012.05.52.png?dl=0
(但同样,我不知道如何创建多页 EPS)
使用 TIFF 文件我得到相同的结果:
https://www.dropbox.com/s/eid1zstbm954121/Screenshot%202019-04-16%20at%2012.11.59.png?dl=0
这不一定是因为打印软件添加了填充 - 现在的数字实际上更小了,因为它是 1929 × 2655 像素,300dpi,这使它成为 6.43 × 8.85 英寸 - 肯定比 A4 小。
编辑:当然,这可以通过使用“适合页面”选项打印来解决,但这可以与任何具有 A4 proportions 的图像一起使用,我想要控制超过尺寸,如果可能的话。
【问题讨论】:
-
如果要定义图形的大小,则不能使用
bbox_inches='tight', pad_inches=0,因为这会有效地改变图形大小。那么您可以删除它并重新提出问题吗? -
感谢@ImportanceOfBeingErnest 的评论如果我在没有
bbox_inches='tight', pad_inches=0选项的情况下保存,则页面上 的图形大小相同(至少大致上,可能是艺术家围绕轴进行了一些更改),只是添加了填充以适合 A4 页面。不过我已经在文中说明了。 -
我想我不明白这个问题。如果保存时不使用
bbox_inches,则图形将完全是A4大小。这有什么问题? -
嗨,很抱歉,如果它令人困惑 - 我不确定我自己是否理解它 - 这就是我找不到解决方案的原因。你是对的,你生成的图形大小正好是 A4,当你在 matplotlib 中时,但是当它变成 PDF 文件(也有 TIFF,记住)时,它会缩小,无论你使用 @ 987654334@ 与否。不同之处在于 PDF 文档页面是 A4 纸,图形周围有填充(没有
bbox_inches)或更小(有bbox_inches='tight'),没有填充。但是图形艺术家的边框永远不会像 A4 那样到达 PDF 页面的边缘。 -
我从您的代码中删除了
box_inches='tight', pad_inches=0并运行了它。我在 pdf 查看器中打开生成的 pdf 并单击“属性”。正如预期的那样,报告的页面大小为 8.27 x 11.69 英寸。
标签: python pdf matplotlib figure