Click here to Skip to main content
15,881,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
XML
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



XML
<%@ 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

>

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


}

}
Posted
Comments
Torakami 10-Oct-14 5:16am    
from my way 2 , there is only one line which i am not getiing ,.,,

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

do i need to give server location path here or something else ??

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:

static void HtmlToPdf(string website,string destinationFile)
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = "wkhtmltopdf.exe";
        startInfo.Arguments = website+" "+destinationFile;
        Process.Start(startInfo);
    }
 
Share this answer
 
Comments
Torakami 10-Oct-14 5:34am    
Hey thanks brother .. but little more help i want , can you please tell me about your method parameters ..

string website,string destination File ..its a request .. Please explain me this in bit detail

what should be this ??
MukeshSagar 10-Oct-14 6:25am    
The first parameter is the name of the page that is to be converted whether it is *.aspx or *.html; the second parameter is the name of file by which you want to save the file
MukeshSagar 10-Oct-14 6:26am    
The first parameter is the name of the file/page that is to be converted to the pdf file, the second parameter is name of the file by which it is to be saved in pdf format
Torakami 10-Oct-14 6:35am    
ok ..please let me try this .. i might need your more help .. as a matter of this is very urgent ... thanks man
Torakami 10-Oct-14 6:37am    
but which path do i need to give for the first parameter, as I am binding records into it .. so do i need to give url of that page which shows me the output of the page .. ??

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");
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900