【问题标题】:Generate PDF using Reportlab with custom size page and best image resolution使用具有自定义尺寸页面和最佳图像分辨率的 Reportlab 生成 PDF
【发布时间】:2018-01-31 23:32:06
【问题描述】:

我有一个生成的图像(使用 PIL),我必须创建一个特定大小的 PDF,它将包含这个(全尺寸)图像。

我从size= 150mm x 105mm开始 我生成了对应的图片1818px x 1287px(带小边框) (mm 到 px,300dpi) 我用这个代码

pp = 25.4  # 1 pp = 25,4mm
return int((dpi * mm_value) / pp)

现在我必须创建大小为 page = 150mm x 105mm 的 PDF 文件 我使用reportlab,我会使用最佳图像质量(打印)的pdf。

可以指定吗?

用这个来创建 PDF 页面大小是正确的:

W = ? // inch value??
H = ? // inch value??
buffer = BytesIO()
p = canvas.Canvas(buffer)
p.setPageSize(size=(W, H)) 

并绘制图像:

p.drawImage(img, 0, 0, width=img.width, preserveAspectRatio=True, mask='auto', anchor='c')

【问题讨论】:

    标签: python django pdf python-imaging-library reportlab


    【解决方案1】:

    诀窍是在将图像绘制到其上之前缩放 reportlab 的画布。它似乎没有正确地从文件中提取 DPI 信息。

    这个示例代码非常适合我的激光打印机:

    from PIL import Image, ImageDraw, ImageFont
    import reportlab.pdfgen.canvas
    from reportlab.lib.units import mm
    
    # Create an image with 300DPI, 150mm by 105mm.
    dpi = 300
    mmwidth = 150
    mmheight = 105
    pixwidth = int(mmwidth / 25.4 * dpi)
    pixheight = int(mmheight / 25.4 * dpi)
    im = Image.new("RGB", (pixwidth, pixheight), "white")
    dr = ImageDraw.Draw(im)
    dr.rectangle((0, 0, pixwidth-1, pixheight-1), outline="black")
    dr.line((0, 0, pixwidth, pixheight), "black")
    dr.line((0, pixheight, pixwidth, 0), "black")
    dr.text((100, 100), "I should be 150mm x 105mm when printed, \
        with a thin black outline, at 300DPI", fill="black")
    # A test patch of 300 by 300 individual pixels, 
    # should be 1 inch by 1 inch when printed,
    # to verify that the resolution is indeed 300DPI.
    for y in range(400, 400+300):
        for x in range(500, 500+300):
            if x & 1 and y & 1:
                dr.point((x, y), "black")
    im.save("safaripdf.png", dpi=(dpi, dpi))
    
    # Create a PDF with a page that just fits the image we've created.
    pagesize = (150*mm, 105*mm)
    c = reportlab.pdfgen.canvas.Canvas("safaripdf.pdf", pagesize=pagesize) 
    c.scale(0.24, 0.24) # Scale so that the image exactly fits the canvas.
    c.drawImage("safaripdf.png", 0, 0) # , width=pixwidth, height=pixheight)
    
    c.showPage()
    c.save()
    

    您可能需要稍微调整比例值,以使尺寸完全适合您的打印机,但上面的值非常接近。我已经用尺子检查过了;-)

    【讨论】:

    • 画布缩放中的0.24从何而来?你从哪里得到这个号码的?
    • 反复试验。但我认为它必须是可计算的。
    猜你喜欢
    • 2013-11-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 2011-03-04
    • 2011-05-08
    相关资源
    最近更新 更多