1
votes

I am facing a strange issue while generating a pdf from html, I am using wkhtmltopdf utility to convert html to pdf successfully in the past but now I need to include page no at footer of every page.I have achieved this by created html file containing java script that read the page parameter and display page no at footer(Html attached). The problem I am facing is with deployment, I am hosting this application on iis, works fine for any arbitrary location but my application is for some reasons has to deploy under program files in c:\ drive in windows where it doesn't work. I am using asp.net to call the utility exe and pass parameters like:

if (!string.IsNullOrEmpty(document.HeaderUrl))
{
paramsBuilder.AppendFormat("--header-html {0} ", document.HeaderUrl);
paramsBuilder.Append("--margin-top 25 ");
paramsBuilder.Append("--header-spacing 5 "); 
}
if (!string.IsNullOrEmpty(document.FooterUrl))
{
paramsBuilder.AppendFormat("--footer-html {0} ", document.FooterUrl);
paramsBuilder.Append("--margin-bottom 25 ");
paramsBuilder.Append("--footer-spacing 5 ");
}

My HTMl to generate page no:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <div style="bottom:0px;right:0px;">
        Page <span id='page'></span> of
        <span id='topage'></span>
    </div>
    <script type="text/javascript">
        var vars = {};
        var x = window.location.search.substring(1).split('&');
        for (var i in x) {
            var z = x[i].split('=', 2);
            vars[z[0]] = unescape(z[1]);
        }
        document.getElementById('page').innerHTML = vars.page;
        document.getElementById('topage').innerHTML = vars.topage;
    </script>
</body>
</html>

to utility. When I am not using footer html everything works but the issue start appearing once it has footer html url. please note the footer html path is absolute and I have tried with giving everyone permission also on the application root level folder. if I host application outside c drive everything works smoothly. Error logged is "Html to PDF conversion of 'C:\Temp\9dd853dc-bf32-441e-8edf-030daf774f95.html' failed. Wkhtmltopdf output: --margin-bottom specified in incorrect location" I am assuming the issue should be more related to permission rather than code but till now I am not able to figure it out

1

1 Answers

0
votes

After spending couple of hours more on the issue I am able to find the exact problem area. Its the way I am creating the input argument parameters for wkhtmltopdf utility. My application is hosted under c:\program files (x86) notice "program files" have space in between them there for my .exe is assuming it to be two different parameters. I fixed it by changing my code to:

if (!string.IsNullOrEmpty(document.HeaderUrl))
            {
                paramsBuilder.AppendFormat("--header-html \"{0}\" ", document.HeaderUrl);
                paramsBuilder.Append("--margin-top 25 ");
                paramsBuilder.Append("--header-spacing 5 "); 
            }
            if (!string.IsNullOrEmpty(document.FooterUrl))
            {
                paramsBuilder.AppendFormat("--footer-html \"{0}\" ", document.FooterUrl);
                paramsBuilder.Append("--margin-bottom 25 ");
                paramsBuilder.Append("--footer-spacing 5 ");               
            }