与wkhtmltopdf.exe路径问题HTML文件转换为PDF [英] Path issue with wkhtmltopdf.exe to convert HTML file to PDF

查看:687
本文介绍了与wkhtmltopdf.exe路径问题HTML文件转换为PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 wkhtmltopdf 来的HTML文件转换成PDF文档的链接按钮
HTTP://$c$c.google.com/p/wkhtmltopdf/

当用户点击运行以下code如下所示,code在传文件的路径作为参数的ProcessStartInfo一个链接按钮。这个code正常工作在下列情况下,只有

考虑到网站上域 http://www.xyz.net/


  1. 当我提到路径 http://demo.XYZ.net/ 它正常工作

  2. 当我提到路径 http://www.XYZ.net/ 它不工作

  3. 在情况下本地主机的正常工作,如果路径是的http://本地主机:51005 / XYZ / 的http://网络:8080 /

有关这个能正常工作,我们需要给网站充分的信任水平和放大器;我不知道为什么code不跑我给它同一个域路径如果我创建放 PrintArticle.aspx 如果我创建一个子域,那么它会工作精细。我NOST知道这是一个安全问题还是什么

code以下

 保护无效lnkbtnDownload_Click(对象发件人,EventArgs的发送)
{
    // ConvertURLToPDF();
    尝试
    {
        字符串的URL =?PrintArticle.aspx条款ArticleID =+请求[条款ArticleID] +&放大器;下载= YES&安培; LANGUAGE =+请求[语言];        //字符串ARGS =的String.Format(\\{0} \\ - ,http://demo.XYZ.net/+网址); //作品
        //字符串ARGS =的String.Format(\\{0} \\ - ,http://www.xyz.net/+网址);不工作
        //字符串ARGS =的String.Format(\\{0} \\ - ,URL);        字符串ARGS =的String.Format(\\{0} \\ - ,HTTP://本地主机:51005 / XYZ /+网址); //作品        VAR的StartInfo =新的ProcessStartInfo(使用Server.Mappath(本\\\\ wkhtmltopdf.exe),参数)
        {
            UseShellExecute =假,
            CreateNoWindow = TRUE,
            RedirectStandardOutput =真
        };
        VAR PROC =新的Process {StartInfo的=的StartInfo};
        proc.Start();        字符串输出= proc.StandardOutput.ReadToEnd();
        字节[]缓冲= proc.StandardOutput.CurrentEncoding.GetBytes(输出);
        proc.WaitForExit();
        proc.Close();
        Response.ContentType =应用程序/ PDF
        Response.AddHeader(内容处置,附件;文件名= download.pdf);
        Response.BinaryWrite(缓冲液);
        到Response.End();
    }
    赶上(异常前)
    {
        扔恩;
    }
}

在案件档案错误消息是在同一个域


  

在'/'应用程序的服务器错误。资源无法找到。


  
  

说明:HTTP 404。您正在寻找(或一个资源的
  依赖)可能已被删除,更名,或
  暂时不可用。请检查以下URL并确保
  它拼写正确。
  请求的URL:/PrintArticle.aspx


  
  

版本信息:Microsoft .NET Framework版本:4.0.30319;
  ASP.NET版本:4.0.30319.272



解决方案

我用下面的语句解决了这个问题。

  VAR URL = Request.Url.GetLeftPart(UriPartial.Authority)+/PrintArticle.aspx?articleID=+请求[条款ArticleID] +&放大器;下载= YES&安培; LANGUAGE =+请求[语言];

现在它工作正常,我不知道什么时候我指定的文件路径它不工作。

I am using wkhtmltopdf to convert HTML file into PDF document on a link Button http://code.google.com/p/wkhtmltopdf/

When User Click on a link Button it runs the following code as shown below code in pass file path as an argument ProcessStartInfo. THis code works fine in Following Scenarios only

Taking Into consideration that website is hosted on Domain http://www.xyz.net/

  1. When i mention path as http://demo.XYZ.net/ It works fine
  2. When i mention path as http://www.XYZ.net/ It doesn't work
  3. In-case of local-host it works fine if path is http://localhost:51005/XYZ/or http://web:8080/

For this to work properly we need to give the website full trust level & i am not sure why code doesn't run i give it the same domain path if i create put PrintArticle.aspx if i create a sub domain then it will work fine. I am nost sure if this is a security problem or what

Code Below

protected void lnkbtnDownload_Click(object sender, EventArgs e)
{
    //ConvertURLToPDF();
    try
    {
        string url = "PrintArticle.aspx?articleID=" + Request["articleID"] + "&download=yes&Language=" + Request["Language"];

        //string args = string.Format("\"{0}\" - ", "http://demo.XYZ.net/" + url); //Works
        //string args = string.Format("\"{0}\" - ", "http://www.xyz.net/" + url); Doesnt work
        //string args = string.Format("\"{0}\" - ", url);

        string args = string.Format("\"{0}\" - ", "http://localhost:51005/XYZ/" + url); //Works

        var startInfo = new ProcessStartInfo(Server.MapPath("bin\\wkhtmltopdf.exe"), args)
        {
            UseShellExecute = false,
            CreateNoWindow = true,
            RedirectStandardOutput = true
        };
        var proc = new Process { StartInfo = startInfo };
        proc.Start();

        string output = proc.StandardOutput.ReadToEnd();
        byte[] buffer = proc.StandardOutput.CurrentEncoding.GetBytes(output);
        proc.WaitForExit();
        proc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment;filename=download.pdf");
        Response.BinaryWrite(buffer);
        Response.End();
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

Error Message in case file is on same domain

Server Error in '/' Application. The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. Requested URL: /PrintArticle.aspx

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

解决方案

I resolved this issue by using the following statement

var url = Request.Url.GetLeftPart(UriPartial.Authority) + "/PrintArticle.aspx?articleID=" + Request["articleID"] + "&download=yes&Language=" + Request["Language"];

Now it is working fine i am not sure what it doesn't work when i specify the file path.

这篇关于与wkhtmltopdf.exe路径问题HTML文件转换为PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆