使用python wkhtmltopdf库将html转换为png

2024-05-15 15:55:10 发布

您现在位置:Python中文网/ 问答频道 /正文

在我的程序中,我试图通过在HTML文件中添加屏幕截图,将HTML页面转换为.png文件。 在我的系统中,我安装了python 3.8wkhtmltopdf library 并将这两个命令添加到path变量。 下面给出了我的python脚本

import sys
import imgkit
options ={'crop-h': '200','crop-w': '375','crop-x': '0','crop-y': '0','disable-smart-width': '','zoom':1.0}
imgkit.from_file(sys.argv[1],sys.argv[2], options=options) 

我得到一个屏幕短如下所示。但是图像丢失了,这个数字实际上需要在右边有一个条形码,在上面有一个标志。它在最终输出中不可见。我有

image after html to png convertion

我尝试从控制台执行此操作,但得到以下错误。 Error message 在我的java程序中,我使用以下代码调用这个程序

String[] cmd = { "python", htmlToPdfScriptLocation, tempHTML.getAbsolutePath(), tempImagePath };
Process p = Runtime.getRuntime().exec(cmd);
try {
        p.waitFor();// wait until the image generation process has terminated
        logger.debug("HTML to img conversion sucessfull.");
        } catch (InterruptedException e) {  
            e.printStackTrace();
            logger.debug("ERROR in html to img Conversion.");
        }

没有错误的。唯一的问题是条形码和徽标不可见。 问题是

  1. 为什么不显示徽标和条形码。所有图像都以html格式正确显示
  2. 为什么在徽标和条形码中阻止访问。这些都是从程序本身创建的。 下面给出了确切需要的数字。 exactly needed

Tags: 文件图像cropimport程序屏幕html错误