简体   繁体   English

如何从.aspx页面生成PDF

[英]how to generate PDF from .aspx page

i want to create PDF, from a currently running .aspx page and i successfully done this. 我想从当前运行的.aspx页面创建PDF,并且我成功地做到了这一点。 But problem is that when i login through a login page and go for to create a PDF of a report_page(i can access this report_page only when i successfully login) and click on button to generate a PDF of currently running report_page then here PDF is generating of a login page rather than report_page. 但是问题是,当我通过登录页面登录并创建一个report_page的PDF时(只有我成功登录后才能访问此report_page),然后单击按钮生成当前正在运行的report_page的PDF,然后在这里生成PDF登录页面(而不是report_page)。 And here i have another report_page which need not to login to generate report when i am generating report through this report_page(not need to login) and try to convert it to PDF then it is working correctly means here PDF is generated of report_page. 在这里,我还有另一个report_page,当我通过此report_page生成报告(无需登录)并尝试将其转换为PDF时,无需登录即可生成报告,然后它可以正常工作,这意味着在此生成了report_page的PDF。

here i paste my code please check it and give me solution for this: 在这里我粘贴我的代码,请检查一下并为此提供解决方案:

 protected void btn_print_Click(object sender, EventArgs e)
    {
        try
        {
            string url = HttpContext.Current.Request.Url.AbsoluteUri;
            int width = 850;
            int height = 550;
            Thumbnail1 thumbnail = new Thumbnail1(url, 990, 1000, width, height);
            Bitmap image = thumbnail.GenerateThumbnail();
            image.Save(Server.MapPath("~") + "/Dwnld/Thumbnail.bmp");
            imagepath = Server.MapPath("~").ToString() + "\\Dwnld\\" + "Thumbnail.bmp";
            imagepath1 = Server.MapPath("~").ToString() + "\\Dwnld\\" + "Thumbnail.pdf";
            convetToPdf();
        }
        catch (Exception)
        {

            throw;
        }
    }



string imagepath = null;
    string imagepath1 = null;
    public void convetToPdf()
    {
        PdfDocument doc = new PdfDocument();
        System.Drawing.Size size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4);
        PdfPage pdfPage = new PdfPage();
        pdfPage.Orientation = PageOrientation.Landscape;
        doc.Pages.Add(pdfPage);
        //  XSize size = PageSizeConverter.ToSize(PdfSharp.PageSize.A4)
        XGraphics xgr = XGraphics.FromPdfPage(doc.Pages[0]);
        XImage img = XImage.FromFile(imagepath);
        xgr.DrawImage(img, 0, 0);
        doc.Save(imagepath1);
        xgr.Dispose();
        img.Dispose();
        doc.Close();
        Response.ContentType = "Application/pdf";
        //Get the physical path to the file.
        string FilePath = imagepath1;
        //Write the file directly to the HTTP content output stream.
        Response.WriteFile(FilePath);
        Response.End();
    }
    public class Thumbnail1
    {
        public string Url { get; set; }
        public Bitmap ThumbnailImage { get; set; }
        public int Width { get; set; }
        public int Height { get; set; }
        public int BrowserWidth { get; set; }
        public int BrowserHeight { get; set; }

        public Thumbnail1(string Url, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
        {
            this.Url = Url;
            this.BrowserWidth = BrowserWidth;
            this.BrowserHeight = BrowserHeight;
            this.Height = ThumbnailHeight;
            this.Width = ThumbnailWidth;
        }
        public Bitmap GenerateThumbnail()
        {
            Thread thread = new Thread(new ThreadStart(GenerateThumbnailInteral));
            thread.SetApartmentState(ApartmentState.STA);
            thread.Start();
            thread.Join();
            return ThumbnailImage;
        }
        private void GenerateThumbnailInteral()
        {
            WebBrowser webBrowser = new WebBrowser();
            webBrowser.ScrollBarsEnabled = false;
            webBrowser.Navigate(this.Url);
            webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WebBrowser_DocumentCompleted);
            while (webBrowser.ReadyState != WebBrowserReadyState.Complete) System.Windows.Forms.Application.DoEvents();
            webBrowser.Dispose();
        }
        private void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            WebBrowser webBrowser = (WebBrowser)sender;
            webBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
            webBrowser.ScrollBarsEnabled = false;
            this.ThumbnailImage = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
            webBrowser.BringToFront();
            webBrowser.DrawToBitmap(ThumbnailImage, webBrowser.Bounds);
            this.ThumbnailImage = (Bitmap)ThumbnailImage.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
        }
    }
    protected void CreateThumbnailImage(object sender, EventArgs e)
    {

    }

The issue is that you are requesting the page-to-be-generated in a separate request which does not include your login cookie/session. 问题是您在单独的请求中请求要生成的页面,其中不包括您的登录cookie /会话。 It is requesting it as though it was a completely separate browser would. 它要求它好像它是一个完全独立的浏览器一样。

The only way I can think is that if you have basic authentication turned on in IIS you can change the url to be: http://myThumbnailUser:myThumbnailPassw0rd@www.mydomain.com/path/to/page/to/be/rendered.aspx . 我能想到的唯一方法是,如果在IIS中打开了基本身份验证,则可以将URL更改为: http://myThumbnailUser:myThumbnailPassw0rd@www.mydomain.com/path/to/page/to/be/rendered.aspx

You could try and attach the method that does all the generation to the page unload event to allow you to get the response stream, pump it into the WebBrowser control and thenuse that to generate your PDF. 您可以尝试将完成所有生成的方法附加到页面卸载事件,以允许您获取响应流,将其泵送到WebBrowser控件中,然后使用它生成PDF。

protected void btn_print_Click(object sender, EventArgs e)
{
    try
    {
        int width = 850;
        int height = 550;

        this.Page.Unload += delegate {
            Thumbnail1 thumbnail = new Thumbnail1(this.Page, 990, 1000, width, height);
            Bitmap image = thumbnail.GenerateThumbnail();
            image.Save(Server.MapPath("~") + "/Dwnld/Thumbnail.bmp");
            imagepath = Server.MapPath("~").ToString() + "\\Dwnld\\" + "Thumbnail.bmp";
            imagepath1 = Server.MapPath("~").ToString() + "\\Dwnld\\" + "Thumbnail.pdf";
            convetToPdf();
        }
    }
    catch (Exception)
    {

        throw;
    }
}

...

public Thumbnail1(Page page, int BrowserWidth, int BrowserHeight, int ThumbnailWidth, int ThumbnailHeight)
    {
        this.Page = page;
        this.BrowserWidth = BrowserWidth;
        this.BrowserHeight = BrowserHeight;
        this.Height = ThumbnailHeight;
        this.Width = ThumbnailWidth;
    }

...

private void GenerateThumbnailInteral()
    {
        WebBrowser webBrowser = new WebBrowser();
        webBrowser.ScrollBarsEnabled = false;
        webBrowser.Navigate(this.Url);
        webBrowser.DocumentStream = this.Page.Response.GetResponseStream()
        webBrowser.ClientSize = new Size(this.BrowserWidth, this.BrowserHeight);
        webBrowser.ScrollBarsEnabled = false;
        this.ThumbnailImage = new Bitmap(webBrowser.Bounds.Width, webBrowser.Bounds.Height);
        webBrowser.BringToFront();
        webBrowser.DrawToBitmap(ThumbnailImage, webBrowser.Bounds);
        this.ThumbnailImage = (Bitmap)ThumbnailImage.GetThumbnailImage(Width, Height, null, IntPtr.Zero);
    }

That also might work.. 那也可能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM