About html pdf file generation

Source: Internet
Author: User

I have seen some ways to generate PDF files through C # code. The most popular itext can generate PDF files on HTML pages (some people have installed the ghost printer on their machines, however, I personally think that the method of generating PDF using itext is complicated, another plug-in is available to better control html pdf file generation. The specific method is described as follows:

First, download an abcpdf. Net 7.0, http://www.oyksoft.com/soft/8576.html, and install it later. In addition, you need to register it during installation.

Find the abcpdf. dll file in the installation directory and add the class library through vs2008.

Finally, it is used in the project, as follows:

1. Put a button on the page where you want to generate a PDF file. The button method is as follows:
Protected void lb1__click (Object sender, eventargs E)
{

String name = request. querystring ["name"];
Response. Redirect ("print. ashx? Url = "+ request. url. tostring () +" & name = "+ name +" & Group = "+ group );
}

2. Create a print. ashx page. The code for this page is as follows:

<% @ Webhandler Language = "C #" class = "print" %>

Using system;
Using system. Web;
Using websupergoo. abc00007;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Text. regularexpressions;
Using system. IO;
Using system. net;
Using system. text;

Public class print: ihttphandler {

Public void processrequest (httpcontext context ){
If (! String. isnullorempty (context. Request. querystring ["url"]) &! String. isnullorempty (context. Request. querystring ["name"])
{
String url = context. Request. querystring ["url"];
Getpdf (URL, context. Request. querystring ["name"], context. Request. querystring ["group"]);
}
}
Public bool isreusable {
Get {
Return false;
}
}
Public void getpdf (string PDF, string name, string Group)
{
Random r = new random ();
Doc thedoc = new doc ();
Thedoc. topdown = true;
Thedoc. rect. String = "22 15 820 580"; // control the displayed size of 205 300 632 895
Thedoc. mediabox. String = "0 0 842 595"; // control the page size
String Reg = @ "/<a id/= (.) +/</a/>"; // This regular expression is used to remove things you do not need to display on the page.
String reg1 = @ "body/{(.) + /}";
String temp = RegEx. Replace (RegEx. Replace (gethtml (PDF), reg1, ""), Reg ,"");
Temp = temp. Replace ("{$ name $}", name. Split (',') [0]). Replace ("{$ group $}", group );
Int theid = thedoc. addimagehtml (temp, true, 0, false );
While (true)
{
If (! Thedoc. chainable (theid ))
{
Break;
}
Thedoc. Page = thedoc. addpage ();
Theid = thedoc. addimagetochain (theid );
}
Byte [] thedata = thedoc. getdata ();
Filecreate (name, thedata );
If (file. exists (httpcontext. Current. server. mappath (name + ". pdf ")))
{
Httpcontext. current. response. write (string. format ("<a target = '_ blank' href = '{0}'> download </a>", name + ". PDF "));
}
}
// Create a file
Public static void filecreate (string name, byte [] datas)
{
Fileinfo createfile = new fileinfo (httpcontext. Current. server. mappath (name + ". pdf"); // create a file
If (createfile. exists)
{
Createfile. Delete ();
}
Filestream FS = createfile. Create ();
FS. Write (datas, 0, datas. Length );
FS. Close ();
}
Private string gethtml (string URL)
{
String strresult = "";
Try
{
Httpwebrequest request = (httpwebrequest) webrequest. Create (URL );
Request. method = "get ";
Httpwebresponse response = (httpwebresponse) request. getresponse ();
Stream streamreceive = response. getresponsestream ();
Streamreader = new streamreader (streamreceive, encoding. utf8 );
Strresult = streamreader. readtoend ();
}
Catch
{
}
Return strresult;
}
}

 

 

This method is used to generate PDF files throughout the project. I personally think this method is very simple. I also often see a lot of people in the forum asking questions about generating PDF files on HTML pages. So today I will share my practices.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.