简体   繁体   English

xhtml2pdf 为本地 html 文件生成空白 pdf

[英]xhtml2pdf generating blank pdf for local html file

I have an interactive html file generated using Bokeh.我有一个使用 Bokeh 生成的交互式 html 文件。 I am trying to convert the html file into pdf using xhtml2pdf but the images on the html file dont show up on the pdf with xhtml2pdf or pdfkit. I am trying to convert the html file into pdf using xhtml2pdf but the images on the html file dont show up on the pdf with xhtml2pdf or pdfkit.

I tried PyQt to generate the pdfs and it worked fine but had issues with being converted to an executable and pdfcrowd worked too but it is a paid library.我尝试使用 PyQt 来生成 pdf,它运行良好,但在转换为可执行文件时遇到了问题,pdfcrowd 也可以运行,但它是一个付费库。 I have looked at all the other solutions online but none of those seem to work.我在网上查看了所有其他解决方案,但似乎没有一个有效。

This is the html file generation:这是 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)

This is the code for converting to pdf这是转换为 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'))

Standard Bokeh plots are JavaScript programs that render to an HTML raster canvas.标准散景图是 JavaScript 程序,它们渲染到 HTML 光栅 canvas。 I am a little surprised to hear that anything works to covert to PDF.听到任何可以隐藏到 PDF 的东西,我有点惊讶。 Any conversion tool that only looks at the base HTML content, without actually executing the JS code, certainly cannot work.任何只查看基本 HTML 内容,而不实际执行 JS 代码的转换工具,肯定是行不通的。 Most likely, PyQt works because it embeds an actual real browser engine, while the others tools do not.最有可能的是,PyQt 可以工作,因为它嵌入了一个真正的浏览器引擎,而其他工具则没有。

You might have better luck by setting output_backend="svg" on your plots (but note there are also some known issues still being fixed with Bokeh's SVG support, so YMMV).通过在绘图上设置output_backend="svg"可能会获得更好的运气(但请注意,Bokeh 的 SVG 支持仍然修复了一些已知问题,所以 YMMV)。 However, I suspect this may also run in to the same problem above, where running JS code is what produces the SVG, and absent a JS engine, no plot will show up.但是,我怀疑这也可能遇到上述同样的问题,运行 JS 代码是产生 SVG 的原因,并且没有 JS 引擎,不会出现 plot。

Ultimately, the only option may be to separately export the Bokeh plots (eg using export_svgs or export_png ) into static PNGs or SVGs that can be included in a PDF (or in an HTML document that then can be naively converted in to a PDF without running JS code). Ultimately, the only option may be to separately export the Bokeh plots (eg using export_svgs or export_png ) into static PNGs or SVGs that can be included in a PDF (or in an HTML document that then can be naively converted in to a PDF without running JS 代码)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM