Wkhtmtopdf -- convert high-resolution HTML to PDF (3)

Source: Internet
Author: User

I browsed a lot of instances and couldn't find a combination of these two requirements, because I could not save HTML as PDF but throw the stream at the same time, let's make a contribution ~~

Next we will select a web page to print, save as PDF, and implement stream throw and save. Suppose we choose"Http://www.cnblogs.com/ITGirl00/"

Page example:

  string fileName = Guid.NewGuid().ToString();
            string outputPath = Server.MapPath("output");
String savepath = string. Format (outputPath + "\" + fileName + ". pdf"); // save
 
            string url = "http://www.cnblogs.com/ITGirl00/";
 
            try
            {
                if (!string.IsNullOrEmpty(url) || !string.IsNullOrEmpty(savepath))
                {
                    Process p = new Process();
                    string resource = HttpContext.Current.Server.MapPath("resoure");
                    string dllstr = string.Format(resource + "\\wkhtmltopdf.exe");
 
                    if (System.IO.File.Exists(dllstr))
                    {
 
 
                        p.StartInfo.FileName = dllstr;
                        p.StartInfo.Arguments = " \"" + url + "\"  \"" + savepath + "\"";
                        p.StartInfo.UseShellExecute = false;
                        p.StartInfo.RedirectStandardInput = true;
                        p.StartInfo.RedirectStandardOutput = true;
                        p.StartInfo.RedirectStandardError = true;
                        p.StartInfo.CreateNoWindow = true;
                        p.Start();
                        p.WaitForExit();
 
                        try
                        {
                            FileStream fs = new FileStream(savepath, FileMode.Open);
                            byte[] file = new byte[fs.Length];
                            fs.Read(file, 0, file.Length);
                            fs.Close();
                            Response.Clear();
Response. AddHeader ("content-disposition", "attachment; filename =" + fileName + ". pdf"); // encode
                            Response.ContentType = "application/octet-stream";
                            Response.BinaryWrite(file);
                        }
                        catch (Exception ee)
                        {
 
                            throw new Exception(ee.ToString());
                        }
 
 
                    }
                }
 
 
            }
            catch (Exception ex)
            {
                throw new Exception(ex.ToString());
            }

 

 

Technorati tags: wkhtmtopdf, HTMLTOPDF, HTML conversion PDF, and outputStream

Related Article

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.