簡體   English   中英

IIS7上的PHP和C#通信(生成動態PDF)

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

背景:

我在C#中找不到任何像樣的免費HTML到PDF轉換實用程序。 對於PHP來說,其中有100多種語言具有廣泛的文檔,支持和CSS支持。 所以我正在使用html2ps和html2pdf (php)。

我在IIS7上安裝了PHP 5.2,並且可以很好地創建PDF。

我在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" />

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

並在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 generatePDF.php PDF?

我建議研究一下iTextSharp的iText免費.NET端口(基於Java的PDF庫),然后就可以從等式中刪除php。

有關使用iTextSharp轉換HTML的信息,請參閱此帖子 (使用google找到)

更新資料

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

叫做:

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

該代碼可能不是您所需要的,但是如您所見,您可以使用Server / Page對象呈現結果,這可能是您應該采用的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM