将多行电子表格转换为 pdf 的简洁方法

发布于 2024-10-11 04:31:01 字数 271 浏览 7 评论 0原文

我不是在寻找将 Excel 文件转换为 pdf 的库,有很多可用的库。我正在寻找一种干净的方法将行数多于页面宽度的电子表格转换为 pdf。

这还可以吗?我不认为缩小文本是一个有效的选项,因为它可能达到上限(即 1 pt 字体),并且电子表格中可能有足够的列来实际达到该限制(~30)。

我现在唯一的想法是使页面横向,但是有没有办法让 pdf 显示为“双页”,两个页面都横向显示,并在下面有正确的页面顺序,看起来像一个有凝聚力的电子表格?

还有其他想法吗?或者对我的想法有什么建议?

I'm not looking for a library to convert excel files to pdf, there are plenty of those available. I'm looking for a clean way to convert a spreadsheet with more rows than the width of a page into a pdf.

Can this even be done? I don't consider making the text smaller a valid option because it could feasibly reach an upper limit (i.e. 1 pt font), and there may be enough columns in the spreadsheet to actually reach that limit (~30).

My only idea right now is to make the pages landscape, but is there a way to have the pdf show as "two-up" with both of the pages in landscape and have the proper page ordering underneath to look like a cohesive spreadsheet?

Any other ideas? or suggestions for the idea I have?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

囍孤女 2024-10-18 04:31:01

假设您可以读取 Excel 文件(例如使用 Apache POI),请考虑使用 Apache FOP 使用您定义的自定义纸张尺寸。如果没有卷纸打印机,打印可能会很困难,但在屏幕上显示就可以了。

Assuming you can read the Excel file (for instance with Apache POI), consider writing to the PDF with Apache FOP using a custom paper size that you define. It may be difficult to print without a roll paper printer, but it will display on the screen just fine.

纵山崖 2024-10-18 04:31:01

您看过 JasperReports 吗?它有一个非常强大的模板引擎。

我从来没有像你那样使用过 JasperReports,但他们的专长是动态报告,所以我猜他们知道如何以一种很好的方式处理页面溢出。

Have you looked at JasperReports? It has a pretty strong templating engine.

I've never used JasperReports the way you do, but their specialty is dynamic reports so I'd guess they know how to handle page overflows in a nice way.

牵你的手,一向走下去 2024-10-18 04:31:01

这就是我最终所做的。它使用 MacOS 上的 QuickLook 功能来制作 HTML 文件,然后使用 wkhtmltopdf 将 HTML 文件转换为 PDF。

#!/usr/bin/python                                                                                                           
#                                                                                                                           
# convert an excel workbook to a PDF on a Mac                                                                               
#                                                                                                                           
#                                                                                                                           
from subprocess import Popen,call,PIPE
import os, os.path, sys
import xml.dom.minidom
import plistlib

if len(sys.argv)==1:
    print("Usage: %s filename.xls output.pdf" % sys.argv[0])
    exit(1)

if os.path.exists("xdir"):
    raise RuntimeError,"xdir must not exists"
os.mkdir("xdir")
call(['qlmanage','-o','xdir','-p',sys.argv[1]])

# Now we need to find the sheets and sort them.                                                                             
# This is done by reading the property list                                                                                 
qldir = sys.argv[1] + ".qlpreview"
propfile = open("%s/%s/%s" % ('xdir',qldir,'PreviewProperties.plist'))
plist = plistlib.readPlist(propfile)
attachments = plist['Attachments']
sheets = []
for k in attachments.keys():
    if k.endswith(".html"):
        basename = os.path.basename(k)
        fn = attachments[k]['DumpedAttachmentFileName']
        print("Found %s -> %s" % (basename,fn))
        sheets.append((basename,fn))
sheets.sort()

# Use wkhtmltopdf to generate the PDF output                                                                                
os.chdir("%s/%s" % ('xdir',qldir))
cmd = ['wkhtmltopdf'];
for (basename,fn) in  sheets:
    cmd.append(fn)
cmd.append("../../" + sys.argv[2])
try:
    call(cmd)
except OSError:
    print("\n\nERROR: %s is not installed\n\n" % (cmd[0]))
    exit(1)
os.chdir("../..")
call(['/bin/rm','-rf','xdir'])

Here's what I ended up doing. It uses the QuickLook feature on MacOS to make a HTML file, then uses wkhtmltopdf to turn the HTML file into a PDF.

#!/usr/bin/python                                                                                                           
#                                                                                                                           
# convert an excel workbook to a PDF on a Mac                                                                               
#                                                                                                                           
#                                                                                                                           
from subprocess import Popen,call,PIPE
import os, os.path, sys
import xml.dom.minidom
import plistlib

if len(sys.argv)==1:
    print("Usage: %s filename.xls output.pdf" % sys.argv[0])
    exit(1)

if os.path.exists("xdir"):
    raise RuntimeError,"xdir must not exists"
os.mkdir("xdir")
call(['qlmanage','-o','xdir','-p',sys.argv[1]])

# Now we need to find the sheets and sort them.                                                                             
# This is done by reading the property list                                                                                 
qldir = sys.argv[1] + ".qlpreview"
propfile = open("%s/%s/%s" % ('xdir',qldir,'PreviewProperties.plist'))
plist = plistlib.readPlist(propfile)
attachments = plist['Attachments']
sheets = []
for k in attachments.keys():
    if k.endswith(".html"):
        basename = os.path.basename(k)
        fn = attachments[k]['DumpedAttachmentFileName']
        print("Found %s -> %s" % (basename,fn))
        sheets.append((basename,fn))
sheets.sort()

# Use wkhtmltopdf to generate the PDF output                                                                                
os.chdir("%s/%s" % ('xdir',qldir))
cmd = ['wkhtmltopdf'];
for (basename,fn) in  sheets:
    cmd.append(fn)
cmd.append("../../" + sys.argv[2])
try:
    call(cmd)
except OSError:
    print("\n\nERROR: %s is not installed\n\n" % (cmd[0]))
    exit(1)
os.chdir("../..")
call(['/bin/rm','-rf','xdir'])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文