使用wkhtmltopdf将Asp.net aspx页面(HTML)转换为pdf [英] Convert Asp.net aspx pages (HTML) to pdf using wkhtmltopdf

查看:97
本文介绍了使用wkhtmltopdf将Asp.net aspx页面(HTML)转换为pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm trying to convert single asp.net page to pdf. Based on my research, most of people said that it can be done using wkhtmltopdf<pre lang="text"></pre>

I've downloded the wkhtmltox0.dll from here http://code.google.com/p/wkhtmltopdf/downloads/list . I am working in asp.net and i can't find any example of how to use it!

Does anybody can provide me an example?

I tried with itextsharp dll but that doesjnt support css and many so i switched to this ..

I will show you what i tried so for with both possibilities










<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ObjectivesPdfDocument.ascx.cs" Inherits="ActusAppraisalSystem.Controls.ObjectivesPdfDocument" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajax" %>
<link href="../css/reporttablestyle.css" rel="stylesheet" type="text/css" />

<link href="../../css/chosen.css" rel="stylesheet" />
<script src="../../js/jquery-1.10.2.js"></script>
<script src="../../js/chosen.jquery.js"></script>

<h2>
    <img alt="" src="../../logos/20130305120113.jpg" />

</h2>


<div class="FrameWrapper frameheight" id="pnlPerson" runat="server">
    <div class="ObjectiveWrapper">
        <div class="Form_Wrap_Left">
            <div class="form_lft_inner" id="divCompany" runat="server">
                <div class="TwoColumn_Form_Wrap_Lft">
                    Objectives Report
                </div>
                <div class="TwoColumn_Form_Wrap_Rht">
                </div>
            </div>

            <div class="form_lft_inner">
                <div class="TwoColumn_Form_Wrap_Lft">
                    Michael O’Sullivan
                </div>
                <div class="TwoColumn_Form_Wrap_Rht">
                </div>
            </div>

            <div class="form_lft_inner" runat="server" id="divManager">
                <div class="TwoColumn_Form_Wrap_Lft">
                    Manager: Lucinda Carney
                </div>
                <div class="TwoColumn_Form_Wrap_Rht">
                </div>
            </div>
        </div>


        <div class="Form_Wrap_Right">
            <div class="Form_rght_inner">
                <div class="TwoColumn_Form_Wrap_Lft">
                    20013/14 {in Arial Bold 14pts}
                </div>
                <div class="TwoColumn_Form_Wrap_Rht">
                </div>
            </div>

            <div class="Form_rght_inner">
                <div class="TwoColumn_Form_Wrap_Lft">
                </div>
                <div class="TwoColumn_Form_Wrap_Rht">
                </div>
            </div>

            <div class="Form_rght_inner">
                <div class="TwoColumn_Form_Wrap_Lft">
                </div>
                <div class="TwoColumn_Form_Wrap_Rht">
                </div>
            </div>
        </div>
        <div style="clear: both;">&nbsp;</div>

        [Objective Title]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Status]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; [Target Date]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {all in Arial Bold 12pts}<br />
        <br />
        <%--<asp:Panel ID="Panel1" runat="server" Height="101px" Width="619px" BorderStyle="Solid" BorderColor="Black">
            This field should contain all of the text from the [Define Objective] field it should be in Arial size 11 font; left aligned. I would like a thin line around the text as shown. Can this box be a flexible size depending on the amount of text in the ‘define objective’ field – or do we need to make it a fixed size?
        </asp:Panel>--%>

        <table id="Panel3" runat="server" style="border:dotted;height:101px;width:619px;border-style:solid;border-color:black">
            <tr>
                <th>This field should contain all of the text from the [Define Objective] field it should be in Arial size 11 font; left aligned. I would like a thin line around the text as shown. Can this box be a flexible size depending on the amount of text in the ‘define objective’ field – or do we need to make it a fixed size?
                </th>
            </tr>
        </table>


        <br />
        <strong>Milestones</strong>
        <br />
        <br />
        [Date of 1st milestone] [text from 1st Milestone……….]<br />
        <br />
        [Date of 2nd milestone] [text from 2nd Milestone……….]<br />
        <br />
        [Date of 3rd milestone etc etc] [text from 3rd Milestone……….]<br />
        <br />
        <strong>Comments</strong><br />
        <br />
        [Date of 1st comment] [text from 1st comment….]<br />
        <br />
        [Date of 2nd comment] [text from 2nd comment….]<br />
        <br />
        <strong>Completion Review<br />
            <br />
            <asp:Panel ID="Panel2" runat="server" Height="101px" Width="619px" BorderStyle="Solid" BorderColor="Black">
                This field should contain all of the text from the [Define Objective] field it should be in Arial size 11 font; left aligned. I would like a thin line around the text as shown. Can this box be a flexible size depending on the amount of text in the ‘define objective’ field – or do we need to make it a fixed size?

            </asp:Panel>
        </strong>
        <br />
        &nbsp;<div runat="server" id="div2">
            <a href="#item1">
                <asp:Button ID="btnExportToExcel" runat="server" Text="Export To Excel" CssClass="BtnRegular_Popup" OnClick="btnExportToExcel_Click" Style="margin-bottom: 15px;" />
            </a>
        </div>


        <div style="clear: both; margin: 0px 0px 10px 0px">
        </div>
        <div class="ErrorSuccesmsg">
            <asp:Label ID="lblMessage" runat="server" EnableViewState="false"></asp:Label>
        </div>
    </div>
</div



>



代码背后的代码: -



使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Web.UI;

使用ActusLibrary.Utils;

使用System.Text;

使用System.Globalization;

使用iTextSharp.text;

使用iTextSharp.text.pdf;

u sing iTextSharp.text.html.simpleparser;

using System.IO;

using System.Web;

using System.Diagnostics;



namespace ActusAppraisalSystem.Controls

{



public partial class ObjectivesPdfDocument : System.Web.UI.UserControl

{







#region PageLevelEvents



protected void Page_Load(object sender, EventArgs e)

{

Page.Header.Title = \"Reports\";

}



#endregion



protected void btnExportToExcel_Click(object sender, EventArgs e)

{

Way1();



Way2();

}



private void Way2()

{

string myDocumentsPath = \"C:\\Program Files\\w khtmltopdf\\bin\\wkhtmltopdf.exe \";



ProcessStartInfo psi = new ProcessStartInfo(myDocumentsPath, \"./User/PerformancePDF.aspx\");



psi.UseShellExecute = false;



psi.RedirectStandardOutput = true;



psi.RedirectStandardInput = true;



psi.RedirectStandardError = true;



psi.CreateNoWindow = true;



Process myProcess = Process.Start(psi);



myProcess.WaitForExit();



myProcess.Close();



Response.Clear();



Response.AddHeader(\"content-disposition\", \"attachment;filename=abc.pdf\");



Response.ContentType = \"application/pdf\";



Response.WriteFile(\" D:\\bb.pdf\");



Response.End();

}



private void Way1()

{

//Response.ContentType = \"application/pdf\";

//Response.AddHeader(\"content-disposition\", \"attachment;filename=TestPage.pdf\");

//Response.Cache.SetCacheability(HttpCacheability.NoCache);

//StringWriter sw = new StringWriter();

//HtmlTextWriter hw = new HtmlTextWriter(sw);

//this.Page.RenderControl(hw);

//StringReader sr = new StringReader(sw.ToString());

//Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);

//HTMLWorker htmlparser = new HTMLWorker(pdfDoc);

//PdfWriter.GetInstance(pdfDoc, Response.OutputStream);

//pdfDoc.Open();

//htmlparse r.Parse(sr);

//pdfDoc.Close();

//Response.Write(pdfDoc);

//Response.End();

}





}



}


>

my code behind code :-

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI;
using ActusLibrary.Utils;
using System.Text;
using System.Globalization;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html.simpleparser;
using System.IO;
using System.Web;
using System.Diagnostics;

namespace ActusAppraisalSystem.Controls
{

public partial class ObjectivesPdfDocument : System.Web.UI.UserControl
{



#region PageLevelEvents

protected void Page_Load(object sender, EventArgs e)
{
Page.Header.Title = "Reports";
}

#endregion

protected void btnExportToExcel_Click(object sender, EventArgs e)
{
Way1();

Way2();
}

private void Way2()
{
string myDocumentsPath = "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe ";

ProcessStartInfo psi = new ProcessStartInfo(myDocumentsPath, "./User/PerformancePDF.aspx");

psi.UseShellExecute = false;

psi.RedirectStandardOutput = true;

psi.RedirectStandardInput = true;

psi.RedirectStandardError = true;

psi.CreateNoWindow = true;

Process myProcess = Process.Start(psi);

myProcess.WaitForExit();

myProcess.Close();

Response.Clear();

Response.AddHeader("content-disposition", "attachment;filename=abc.pdf");

Response.ContentType = "application/pdf";

Response.WriteFile("D:\\bb.pdf");

Response.End();
}

private void Way1()
{
//Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
//StringWriter sw = new StringWriter();
//HtmlTextWriter hw = new HtmlTextWriter(sw);
//this.Page.RenderControl(hw);
//StringReader sr = new StringReader(sw.ToString());
//Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
//HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
//PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
//pdfDoc.Open();
//htmlparser.Parse(sr);
//pdfDoc.Close();
//Response.Write(pdfDoc);
//Response.End();
}


}

}

推荐答案

khtmltopdf http://www.google.com google.pdf



That is it. You can go to any web page... even aspx. css is supported better than any other utility as it uses the webkit html rendering engine (Safari, Chrome).



There is a single .exe file that can be used from .Net simply by using Process.Start Make sure that you copy the exe into your project directory or you have to specify the full path The sample code is as follows:




That is it. You can go to any web page... even aspx. css is supported better than any other utility as it uses the webkit html rendering engine (Safari, Chrome).

There is a single .exe file that can be used from .Net simply by using Process.Start Make sure that you copy the exe into your project directory or you have to specify the full path The sample code is as follows:

static void HtmlToPdf(string website,string destinationFile)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "wkhtmltopdf.exe";
        startInfo.Arguments = website+" "+destinationFile;
        Process.Start(startInfo);
    }


private void HtmlToPdf(string website, string destinationFile)
        {

            ProcessStartInfo startInfo = new ProcessStartInfo();

            startInfo.UseShellExecute = false;

            startInfo.RedirectStandardOutput = true;

            startInfo.RedirectStandardInput = true;

            startInfo.RedirectStandardError = true;

            startInfo.CreateNoWindow = true;

            startInfo.FileName = "C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe";
            startInfo.Arguments = website + " " + destinationFile;
            Process myProcess = Process.Start(startInfo);
            myProcess.WaitForExit();

            myProcess.Close();

            Response.Clear();

            Response.AddHeader("content-disposition", "attachment;filename=abc.pdf");

            Response.ContentType = "application/pdf";

            Response.WriteFile(destinationFile);
            Response.End();

        }







HtmlToPdf("http://localhost:32370/User/PerformancePDF.aspx", "D:\\New.pdf");


这篇关于使用wkhtmltopdf将Asp.net aspx页面(HTML)转换为pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆