简体   繁体   English

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

[英]PHP and C# communication on IIS7 (to generate dynamic PDF)

Background: 背景:

I couldn't find any decent free HTML to PDF conversion utilities in C#. 我在C#中找不到任何像样的免费HTML到PDF转换实用程序。 There are 100s of them for PHP with extensive documentation, support, and CSS support. 对于PHP来说,其中有100多种语言具有广泛的文档,支持和CSS支持。 So I am using html2ps and html2pdf (php). 所以我正在使用html2ps和html2pdf (php)。

I have PHP 5.2 installed on IIS7 and its working beautifully to create PDFs. 我在IIS7上安装了PHP 5.2,并且可以很好地创建PDF。

I have the following in getPDF.aspx 我在getPDF.aspx具有以下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" />

and in getPDF.aspx.cs : 和在getPDF.aspx.cs

protected void Page_Load(object sender, EventArgs e){
    // AddContentControl simples adds a controls to the content Placeholder.

    AddContentControl("controls/page1.ascx");
    AddContentControl("controls/page2.ascx");
    AddContentControl("controls/page3.ascx");
}

and in generatePDF.php : 并在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 works perfectly...except the output is HTML. -getPDF.aspx可以正常工作...除了输出是HTML之外。

So how can I get getPDF.aspx to output its HTML as PDF generated by generatePDF.php ? 那么,如何获取getPDF.aspx来将其HTML输出为generatePDF.php generatePDF.php PDF?

I'd suggest looking into iTextSharp a free .NET port of iText (Java-Based PDF Lib) Then you can cut php right out of the equation. 我建议研究一下iTextSharp的iText免费.NET端口(基于Java的PDF库),然后就可以从等式中删除php。

For converting HTML using iTextSharp Please See This Post (Found using google) 有关使用iTextSharp转换HTML的信息,请参阅此帖子 (使用google找到)

Update 更新资料

Rendering Partials in ASP.NET Forms (ie rendering a single control, or a page with controls) You create a System.Web.Page to drive the event structure. 以ASP.NET表单呈现部分(即呈现单个控件或带有控件的页面)您可以创建System.Web.Page来驱动事件结构。

Here's a code-sample I adapted for a project of mine: 这是我为我的项目改编的代码样本:

    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();
    }

It's called like this: 叫做:

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

This code may not be what you need, but as you can see you can render a result using the Server / Page objects, this may be the route you should take. 该代码可能不是您所需要的,但是如您所见,您可以使用Server / Page对象呈现结果,这可能是您应该采用的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM