Dynamically generate an HTML page for ASPX and export the Web page to PDF

Source: Internet
Author: User
Tags html header httpcontext

1. First find the Wnvhtmlconvert.dll file and introduce it into the project.

2.server.execute ("pos.aspx?id=" + IDs); Execute the appropriate ASPX Web page

There is one such method in the 3.pos.aspx Web page that is used to generate HTML pages

protected override void Render (HtmlTextWriter writer)
{
String htmlname = "Posdemo";
String fileName = String. Empty;


String content = String. Empty;
System.IO.StringWriter StringWriter = new System.IO.StringWriter ();
HtmlTextWriter htmlwriter = new HtmlTextWriter (StringWriter);
System.IO.StreamWriter SW = null;
Try
{
Renders the contents of the current page into a temporary HtmlTextWriter object
Base. Render (HTMLWriter);
Htmlwriter.close ();
Get the full contents of the current page
Content = Stringwriter.tostring ();
FileName = HTMLName + ". html";
SW = new StreamWriter (Server.MapPath ("htm/") + FileName, false, code);
Sw. Write (str);
Sw. Flush ();
System.IO.StringWriter creaobj = new System.IO.StringWriter (Server.MapPath ("html/") + FileName, false, content);

String path = HttpContext.Current.Server.MapPath (".. /upload/");

System.Text.Encoding code = System.Text.Encoding.GetEncoding ("Utf-8");
SW = new System.IO.StreamWriter (path + fileName, false, code);
SW = new System.IO.StreamWriter (FileName, False, code);
Sw. Write (content);
Sw. Flush ();
String website = "http://" + HttpContext.Current.Request.Url.Authority;

Response.Write (content);
}
Catch {}
Finally
{
Sw. Dispose ();
Stringwriter.dispose ();
Htmlwriter.close ();
Htmlwriter.dispose ();
}

}

4. Use the following class to generate PDF bytes

public class Mypdfgenerator
{
public static byte[] Create (string surl)
{
Try
{
Pdfconverter pdfconverter = new Pdfconverter ();
Set the license key-required
Pdfconverter.licensekey = "p38cbx6aww7b9c81tjegxnrazp+j7rojs+9omj3tuycauk+clwdritm5t59hdw5r";//test key, add by Tieli

Pdfconverter.licensekey = "oyqqgzcblpebly+rgzkqj5ctj5iymjg=";
Set the converter options-optional
PdfConverter.PdfDocumentOptions.PdfPageSize = pdfpagesize.a4;
PdfConverter.PdfDocumentOptions.PdfCompressionLevel = pdfcompressionlevel.nocompression;
PdfConverter.PdfDocumentOptions.PdfPageOrientation = pdfpageorientation.portrait;
PdfConverter.PdfDocumentOptions.ShowHeader = false;
PdfConverter.PdfDocumentOptions.ShowFooter = false;
PdfConverter.PdfDocumentOptions.AutoSizePdfPage = true;
Set to generate selectable PDF or a PDF with embedded image-optional
PDFCONVERTER.PDFDOCUMENTOPTIONS.GENERATESELECTABLEPDF = true;
Set the embedded fonts option-optional, by default is False
PdfConverter.PdfDocumentOptions.EmbedFonts = false;
Enable the live HTTP links option-optional, by default is True
PdfConverter.PdfDocumentOptions.LiveUrlsEnabled = true;
Enable the languages, by default false
pdfconverter.righttoleftenabled = false;


PdfConverter.PdfDocumentInfo.AuthorName = "Telamon Global, Inc.";

Add HTML Header
if (cbaddheader.checked)
AddHeader (Pdfconverter);
Add HTML Footer
if (cbaddfooter.checked)
Addfooter (Pdfconverter);

Performs the conversion and get the PDF document bytes that can further
Save to a file or Send as a browser response
byte[] pdfbytes = pdfconverter.getpdffromurlbytes (sURL);
byte[] Pdfbytes = Pdfconverter.getpdfbytesfromhtmlfile (@ "D:\Peter\Projects\TGSAPSystem\Code\HKOMS\Web site\ Quotes\aaaa.html ");
byte[] pdfbytes = pdfconverter.getpdfbytesfromhtmlstring (HTML);


Get the HTML string for the report
StringWriter htmlstringwriter = new StringWriter ();
Server.Execute ("invoicetemplate.aspx", Htmlstringwriter);
String htmlcodetoconvert = Htmlstringwriter.getstringbuilder (). ToString ();
Htmlstringwriter.close ();

String baseUrl = Getabsoluteurl (URL);

Splendiderror.systemerror (New StackTrace (true). GetFrame (0), BASEURL);
Get the PDF bytes from HTML string
byte[] pdfbytes = pdfconverter.getpdfbytesfromhtmlstring (URL);

return pdfbytes;
}
Catch
{

return null;
}
}
}

5. Calling methods

byte[] bytes = mypdfgenerator.create (Server.MapPath (". /upload/posdemo.html "));

6. Final Export PDF

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
Response. Clear ();
Response. AddHeader ("Content-type", "Binary/octet-stream");
Response. AddHeader ("Content-disposition",
"Attachment; Filename= "+ FileName +". pdf; Size= "+ bytes. Length.tostring ());
Response. Flush ();
Response. BinaryWrite (bytes);
Response. Flush ();
Response. End ();

Dynamically generate an HTML page for ASPX and export the Web page to PDF

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.