Search code examples
pythonhtmlpngwkhtmltopdf

html to png using python wkhtmltopdf library


In my program I am trying to convert HTML page to .png file by tacking a screen shot form the html file. in my System I have installed python 3.8 , wkhtmltopdf library and added these 2 commands to path variable. My python Script given bellow.

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) 

I am getting a screen short given below. but the image was missing in it, the figure actually need a bar code in right and logo on top. it is not visible in the final out put. I have

image after html to png convertion

i tried to do this from console I am getting the error below. Error message from my java program I am calling this program using following code.

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.");
        }

Having no error. the Only issue is in the barcode and logo are not visible. The question is

  1. Why it is not showing logo and barcode. all images are correctly displaying in html.
  2. why the access is blocked into the logos and barcode. those are created from the program itself. The figure exactly needed is given below. exactly needed

Solution

  •   'crop-h': '200','crop-w': '375','crop-x': '0','crop-y': '0','disable-smart-width': '','zoom':1.0, "enable-local-file-access": ''
    }
    

    It is due to the default value of access to the local file is false. The issue can solved by using "enable-local-file-access": ''