2015-11-25 42 views
-4

通過將HTML內容傳遞給方法來創建PDF。 我想這樣做是c#。有沒有我可以使用的工具?通過將HTML內容傳遞給方法來創建PDF

+0

我用Google搜索「c#創建pdf從html」,並找到了一些很好的起點。 –

+0

這個網站開始吸收所有的投票無緣無故。 WTF。 –

+1

這個網站開始吸收所有可以通過搜索網頁來回答的問題。 – TripeHound

回答

0

Essential PDF可以HTML轉換爲PDF。它是用C#編寫的,可以作爲一個庫在任何.NET平臺上使用。如果您有資格,community license提供免費的整套產品。

注意:我爲Syncfusion工作。

0

HiQPdf Software提供了一個free HTML to PDF Converter for .NET,您可以使用它免費製作小型和中型PDF文檔。對於大型PDF或高級功能,您可以隨時升級至專業版。

我是HiQPdf支持代表。該頁面的相關代碼複製如下:

protected void buttonConvertToPdf_Click(object sender, EventArgs e) 
{ 
    // create the HTML to PDF converter 
    HtmlToPdf htmlToPdfConverter = new HtmlToPdf(); 

    // set browser width 
    htmlToPdfConverter.BrowserWidth = int.Parse(textBoxBrowserWidth.Text); 

    // set browser height if specified, otherwise use the default 
    if (textBoxBrowserHeight.Text.Length > 0) 
     htmlToPdfConverter.BrowserHeight = int.Parse(textBoxBrowserHeight.Text); 

    // set HTML Load timeout 
    htmlToPdfConverter.HtmlLoadedTimeout = int.Parse(textBoxLoadHtmlTimeout.Text); 

    // set PDF page size and orientation 
    htmlToPdfConverter.Document.PageSize = GetSelectedPageSize(); 
    htmlToPdfConverter.Document.PageOrientation = GetSelectedPageOrientation(); 

    // set PDF page margins 
    htmlToPdfConverter.Document.Margins = new PdfMargins(0); 

    // set a wait time before starting the conversion 
    htmlToPdfConverter.WaitBeforeConvert = int.Parse(textBoxWaitTime.Text); 

    // convert HTML to PDF 
    byte[] pdfBuffer = null; 

    if (radioButtonConvertUrl.Checked) 
    { 
     // convert URL to a PDF memory buffer 
     string url = textBoxUrl.Text; 

     pdfBuffer = htmlToPdfConverter.ConvertUrlToMemory(url); 
    } 
    else 
    { 
     // convert HTML code 
     string htmlCode = textBoxHtmlCode.Text; 
     string baseUrl = textBoxBaseUrl.Text; 

     // convert HTML code to a PDF memory buffer 
     pdfBuffer = htmlToPdfConverter.ConvertHtmlToMemory(htmlCode, baseUrl); 
    } 

    // inform the browser about the binary data format 
    HttpContext.Current.Response.AddHeader("Content-Type", "application/pdf"); 

    // let the browser know how to open the PDF document, attachment or inline, and the file name 
    HttpContext.Current.Response.AddHeader("Content-Disposition", String.Format("{0}; filename=HtmlToPdf.pdf; size={1}", 
     checkBoxOpenInline.Checked ? "inline" : "attachment", pdfBuffer.Length.ToString())); 

    // write the PDF buffer to HTTP response 
    HttpContext.Current.Response.BinaryWrite(pdfBuffer); 

    // call End() method of HTTP response to stop ASP.NET page processing 
    HttpContext.Current.Response.End(); 
} 
相關問題