html 使用 python wkhtmltopdf 库转为 png
html to png using python wkhtmltopdf library
在我的程序中,我试图通过从 html 文件中添加屏幕截图来将 HTML 页面转换为 .png 文件。
在我的系统中我安装了 python 3.8
, wkhtmltopdf library
并将这两个命令添加到路径变量中。
我的 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)
我的屏幕短路如下。但是里面缺少图像,该图实际上需要右侧的条形码和顶部的徽标。它在最终输出中不可见。我有
我试图从控制台执行此操作,但出现以下错误。
从我的 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.");
}
没有错误。唯一的问题是条形码和徽标不可见。
问题是
- 为什么不显示徽标和条形码。所有图像都正确显示在 html.
- 为什么访问徽标和条形码被阻止。这些是从程序本身创建的。
下面给出了确切需要的数字。
'crop-h': '200','crop-w': '375','crop-x': '0','crop-y': '0','disable-smart-width': '','zoom':1.0, "enable-local-file-access": ''
}
这是因为访问本地文件的默认值为false。使用 "enable-local-file-access": ''
可以解决这个问题
在我的程序中,我试图通过从 html 文件中添加屏幕截图来将 HTML 页面转换为 .png 文件。
在我的系统中我安装了 python 3.8
, wkhtmltopdf library
并将这两个命令添加到路径变量中。
我的 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)
我的屏幕短路如下。但是里面缺少图像,该图实际上需要右侧的条形码和顶部的徽标。它在最终输出中不可见。我有
我试图从控制台执行此操作,但出现以下错误。
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.");
}
没有错误。唯一的问题是条形码和徽标不可见。 问题是
- 为什么不显示徽标和条形码。所有图像都正确显示在 html.
- 为什么访问徽标和条形码被阻止。这些是从程序本身创建的。
下面给出了确切需要的数字。
'crop-h': '200','crop-w': '375','crop-x': '0','crop-y': '0','disable-smart-width': '','zoom':1.0, "enable-local-file-access": ''
}
这是因为访问本地文件的默认值为false。使用 "enable-local-file-access": ''