2015-11-25 39 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(); 
} 
相关问题