我有一个用Bokeh生成的交互式html文件。我正在尝试使用xhtml2pdf将html文件转换为pdf,但是使用xhtml2pdf或pdf工具包时,html文件上的图像无法显示在pdf上。
我试着用PyQt生成pdf,它工作得很好,但在转换成可执行文件时有问题,pdfcrowd也能用,但它是一个付费的库。我已经在网上看过所有其他的解决方案,但似乎都不起作用。
这是html文件的生成:
source = ColumnDataSource(data=dict(x=xx, y=yy, z=val))
surface1 = Surface3d1(x="x", y="y", z="z", data_source=source, width=600, height=600)
surface2 = Surface3d2(x="x", y="y", z="z", data_source=source, width=600, height=600)
surface3 = Surface3d3(x="x", y="y", z="z", data_source=source, width=600, height=600)
surface4 = Surface3d4(x="x", y="y", z="z", data_source=source, width=600, height=600)
l1 = gridplot([[Div(text = 'Side View 1'), Div(text = 'Front View')], [surface1, surface2]])
l2 = gridplot([[Div(text = 'Side View 2'), Div(text = 'Top View')], [surface3, surface4]])
filename = filename_out.split("\\")
layout = column(Div(text = str(filename[-1].replace('_', ' '))),Div(text = 'Absolute Warpage = ' + str(warpage)),l1,l2)
output_file(filename_out[:-1]+'.html', title = 'CMM Scan', mode = 'inline')
save(layout)
这是转换成pdf的代码。
def convert_html_to_pdf(source_html, output_filename):
from xhtml2pdf import pisa
# open output file for writing (truncated binary)
result_file = open(output_filename, "w+b")
# convert HTML to PDF
pisa_status = pisa.CreatePDF(
source_html, # the HTML to convert
dest=result_file) # file handle to recieve result
# close output file
result_file.close() # close output file
# return True on success and False on errors
return pisa_status.err
convert_html_to_pdf(str(filename_out+'.html'),str(filename_out+'.pdf'))
发布于 2020-08-18 02:04:37
标准Bokeh绘图是渲染到HTML光栅画布的JavaScript程序。我有点惊讶地听说,任何工作都可以转换为PDF。任何只看基本HTML内容而不实际执行JS代码的转换工具肯定不能工作。最有可能的是,PyQt之所以能工作,是因为它嵌入了一个真正的浏览器引擎,而其他工具则不能。
在绘图上设置output_backend="svg"
可能会有更好的运气(但请注意,Bokeh支持仍有一些已知问题正在修复,所以YMMV)。但是,我怀疑这也可能会遇到上面的相同问题,运行JS代码是生成SVG的原因,如果没有JS引擎,就不会显示任何图。
最终,唯一的选择可能是将Bokeh图(例如,使用export_svgs
或export_png
)单独导出为静态PNG或SVG,这些静态PNG或SVG可以包含在PDF中(或在HTML文档中,然后可以在不运行JS代码的情况下简单地转换为PDF )。
https://stackoverflow.com/questions/63456063
复制相似问题