Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
IIS7上的PHP和C#通信(生成动态PDF)_C#_.net_Php_Pdf Generation - Fatal编程技术网

IIS7上的PHP和C#通信(生成动态PDF)

IIS7上的PHP和C#通信(生成动态PDF),c#,.net,php,pdf-generation,C#,.net,Php,Pdf Generation,背景: 我在C#中找不到任何像样的免费HTML到PDF转换实用程序。其中有100个用于PHP,具有广泛的文档、支持和CSS支持。所以我使用的是(php) 我已经在IIS7上安装了PHP5.2,它可以很好地创建PDF 我在getPDF.aspx <!-- Output the header --> <DM:header runat="server" ID="header" /> <asp:Placeholder id="content" runat="server"

背景:

我在C#中找不到任何像样的免费HTML到PDF转换实用程序。其中有100个用于PHP,具有广泛的文档、支持和CSS支持。所以我使用的是(php)

我已经在IIS7上安装了PHP5.2,它可以很好地创建PDF

我在
getPDF.aspx

<!-- Output the header -->
<DM:header runat="server" ID="header" />

<asp:Placeholder id="content" runat="server" />

<!-- Output the footer -->
<DM:footer runat="server" ID="footer" />
generatePDF.php
中:

<?php
    /* ... includes and stuff here ... */

    $data = "THE HTML GOES HERE!";
    // creates the PDF from the $data and Outputs the created file.
    convert_to_pdf($data);
?>

-- getPDF.aspx工作得很好…除了输出是HTML

那么我如何获得
getPDF.aspx
将其HTML输出为由
generatePDF.php
生成的PDF?

我建议查看iText(基于Java的PDF库)的免费.NET端口,然后您可以将php从等式中删除

有关使用
iTextSharp
转换HTML的信息,请参阅(使用谷歌搜索)

更新

在ASP.NET窗体中呈现部分(即呈现单个控件或包含控件的页面),您可以创建System.Web.page来驱动事件结构

以下是我为我的一个项目改编的代码示例:

    public static string Render<T>(string controlPath, Action<T> initControlCallback) where T : Control
    {
        Page renderPage = new Page();

        // Load the control & add to page
        T control = (T) renderPage.LoadControl(controlPath);
        renderPage.Controls.Add(control);

        // Initialize the control
        initControlCallback.Invoke(control);
        renderPage.DataBind();

        StringWriter result = new StringWriter();
        HttpContext.Current.Server.Execute(renderPage, result, false); // Render Process
        return result.ToString();
    }
publicstaticstringrender(stringcontrolpath,Action initControlCallback),其中T:Control
{
页面呈现页面=新建页面();
//加载控件并添加到页面
T control=(T)renderPage.LoadControl(controlPath);
renderPage.Controls.Add(控件);
//初始化控件
initControlCallback.Invoke(控件);
renderPage.DataBind();
StringWriter结果=新建StringWriter();
HttpContext.Current.Server.Execute(renderPage,result,false);//呈现过程
返回result.ToString();
}
它的名字是这样的:

MyHelper.Render<MyControlBase>("~/SomePath/SomeControl.ascx", p => { p.SomeProperty = "Initializer" });
MyHelper.Render(“~/SomePath/SomeControl.ascx”,p=>{p.SomeProperty=“Initializer”});

这段代码可能不是您需要的,但正如您所看到的,您可以使用服务器/页面对象呈现结果,这可能是您应该采取的路线。

我以前确实使用过iTextSharp…但我不知道它可以与HTML一起使用。字符串“HTML”不在他们的主页上。我正在处理它。现在我的问题是,我试图呈现控件,然后将其内容写入PDF。控件依赖于它们自己的页面加载事件,在使用control.RenderControl(HtmlTextWriter)时不会触发这些事件;
MyHelper.Render<MyControlBase>("~/SomePath/SomeControl.ascx", p => { p.SomeProperty = "Initializer" });