c# .net 프로젝트를 하고 있는데 견적서 화면을 pdf로 다운로드 받고 싶다는 고객의 요청~ㅠ

 

1. Estimate.aspx (견적서 화면)에 pdf로 출력될 내용을 panel로 감쌈

 

2. CS에 pdf 다운로드 구현 => NReco.PdfGenerator 사용

 

3. "형식 'UpdatePanel'의 컨트롤 'ContentPage_페널명'은(는) runat=server 구문과 함께 form 태그 내부에 와야 합니다." 에러 발생 ㅠ

 

=> c# 코드에 하기의 코드로 해결

        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {
            // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
        }

 

==========================================================================

Estimate.aspx => 코드

==========================================================================

<asp:Panel ID="pnCerti" runat="server" CssClass="certi_area">
            <table border="0" cellpadding="0" cellspacing="0" width="100%" class="inter_margin">
                <tr>
                    <td class="certi_tit3">
                        <h1>견 적 서</h1>
                    </td>
                </tr>
            </table>
            <hr />

            <table border="0" cellpadding="0" cellspacing="0" width="100%" class="tbl_certi">
                <colgroup>
                    <col width="130" />
                    <col width="280" />
                    <col width="" />
                </colgroup>
                <tr>
                    <th style="height:60px;">수&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;신 : </th>
                    <td style="font-weight:bold;">
                        &nbsp;<asp:Label ID="FLD_RECEIVE" runat="server" CssClass="certi_lb" style="font-weight:bold;"></asp:Label> 귀중
                    </td>
                    <td rowspan="6">
                        <table border="0" cellpadding="0" cellspacing="0" style="width:100%; vertical-align:top;">
                            <colgroup>
                                <col width="" />
                            </colgroup>
                            <tr>
                                <td><img src="<%=host%>/Pages__C013/Images/ensecurelogo_small.jpg" width="100" height="47" /></td>

<-- 이미지는 "http://도메인.사이트주소/경로" 요딴식으로 해야 적용됨. -->
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            <asp:UpdatePanel ID="EstimateSubMaintenanceUpdatePanel" runat="server">
                <ContentTemplate>
                    <asp:GridView ID="gvListEstimateSubMaintenance" runat="server" AutoGenerateColumns="false" CssClass="table_content" Width="100%" AllowPaging="false"
                    ShowHeaderWhenEmpty="true" OnRowDataBound = "RowDataBound_EstimateSubMaintenance">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:TemplateField HeaderText="No">
                                <ItemTemplate><span id="dataItemIndex"><%# Container.DataItemIndex + 1 %></span></ItemTemplate>
                                <ItemStyle CssClass="data_AC" />
                                <HeaderStyle CssClass="cell_center" Width="5%" />
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="제품명">
                                <ItemTemplate>
                                    <asp:Label ID="PRODUCTNAME" runat="server" Text='<%# Eval("PRODUCTNAME") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle CssClass="data_AL" />
                                <HeaderStyle Width="15%" />
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="상세내역">
                                <ItemTemplate>
                                    <div style="width: 225px;" title='<%# Eval("REMARK") %>'>
                                        <asp:Label ID="lblREMARK" runat="server" Text='<%# Eval("REMARK") %>'></asp:Label>
                                    </div>
                                </ItemTemplate>
                                <ItemStyle CssClass="data_AL" />
                                <HeaderStyle Width="29%" />
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="수량" HeaderStyle-CssClass="cell_center" HeaderStyle-Width="7%">
                                <ItemTemplate>
                                    <asp:Label ID="VOLUME" runat="server" Text='<%# SetComma(Eval("VOLUME")) %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle CssClass="data_AR" />
                            </asp:TemplateField>
                        </Columns>
                        <HeaderStyle CssClass="cell_center" />
                        <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
                        <RowStyle />
                        <EmptyDataRowStyle CssClass="data_AC row25" />
                        <EmptyDataTemplate>
                            데이터가 존재하지 않습니다.
                        </EmptyDataTemplate>
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>
        </asp:Panel>

 

 

==========================================================================

Estimate.aspx.cs => 코드

==========================================================================

#region PDF 버튼 클릭 이벤트
        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                string fileName = "Estimate_Maintenance_PDF.pdf";
                string TargetFile = Request.PhysicalApplicationPath + "\\Pages__C013\\files\\" + fileName;

                // pdf 파일 저장
                GetDetail(TargetFile);

                Response.ContentType = "application/x-download";
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
                Response.WriteFile(TargetFile);
                Response.Flush();
                Response.End();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        #endregion


        #region html pdf 파일로 저장
        private void GetDetail(string TargetFile)
        {
            string strHtml = string.Empty;
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            pnCerti.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            strHtml = sr.ReadToEnd();
            sr.Close();

            var htmlContent = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>"
                + "<link href='" + host + "/Pages__C013/Styles/AddStyle.css' rel='stylesheet'>"
                + "<link href='" + host + "/Styles/CommonStyle.css' rel='stylesheet'>"
                + "<link href='" + host + "/Styles/layout.css' rel='stylesheet' type='text/css'>"
                + "<link href='" + host + "/Styles/topnavi.css' rel='stylesheet' type='text/css'>"
                + "<link href='" + host + "/Styles/topnavi_tenant.css' rel='stylesheet' type='text/css'>"
                + "<link href='" + host + "/Styles/common.css' rel='stylesheet' type='text/css'>"
                + "<body>" + strHtml + "</body>";

            var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
            var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);


            string ModifiedFileName = string.Empty;
            string FinalFileName = string.Empty;

            File.WriteAllBytes(TargetFile, pdfBytes);
        }

        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {
              // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
        }
        #endregion

 

 

*** 겁나 삽질 끝에 찾은 NReco.PdfGenerator

*** 아주~ 잘된다~

 

 

나와 같이 삽질하는 사람들을 위해서 공유 드립니다.

 

NReco.PdfGenerator.dll


'IT > C#' 카테고리의 다른 글

c# 숫자및 소수점 입력 onkeypress  (0) 2018.01.02
숫자만 입력하게하기  (0) 2017.12.18
로딩바 화면 구현  (0) 2017.01.17
csv 만들기  (0) 2016.12.27
그리드뷰(GridView) 필드 동적 생성  (0) 2015.06.22

+ Recent posts

c# .net 프로젝트를 하고 있는데 견적서 화면을 pdf로 다운로드 받고 싶다는 고객의 요청~ㅠ

 

1. Estimate.aspx (견적서 화면)에 pdf로 출력될 내용을 panel로 감쌈

 

2. CS에 pdf 다운로드 구현 => NReco.PdfGenerator 사용

 

3. "형식 'UpdatePanel'의 컨트롤 'ContentPage_페널명'은(는) runat=server 구문과 함께 form 태그 내부에 와야 합니다." 에러 발생 ㅠ

 

=> c# 코드에 하기의 코드로 해결

        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {
            // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
        }

 

==========================================================================

Estimate.aspx => 코드

==========================================================================

<asp:Panel ID="pnCerti" runat="server" CssClass="certi_area">
            <table border="0" cellpadding="0" cellspacing="0" width="100%" class="inter_margin">
                <tr>
                    <td class="certi_tit3">
                        <h1>견 적 서</h1>
                    </td>
                </tr>
            </table>
            <hr />

            <table border="0" cellpadding="0" cellspacing="0" width="100%" class="tbl_certi">
                <colgroup>
                    <col width="130" />
                    <col width="280" />
                    <col width="" />
                </colgroup>
                <tr>
                    <th style="height:60px;">수&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;신 : </th>
                    <td style="font-weight:bold;">
                        &nbsp;<asp:Label ID="FLD_RECEIVE" runat="server" CssClass="certi_lb" style="font-weight:bold;"></asp:Label> 귀중
                    </td>
                    <td rowspan="6">
                        <table border="0" cellpadding="0" cellspacing="0" style="width:100%; vertical-align:top;">
                            <colgroup>
                                <col width="" />
                            </colgroup>
                            <tr>
                                <td><img src="<%=host%>/Pages__C013/Images/ensecurelogo_small.jpg" width="100" height="47" /></td>

<-- 이미지는 "http://도메인.사이트주소/경로" 요딴식으로 해야 적용됨. -->
                            </tr>
                        </table>
                    </td>
                </tr>
            </table>
            <asp:UpdatePanel ID="EstimateSubMaintenanceUpdatePanel" runat="server">
                <ContentTemplate>
                    <asp:GridView ID="gvListEstimateSubMaintenance" runat="server" AutoGenerateColumns="false" CssClass="table_content" Width="100%" AllowPaging="false"
                    ShowHeaderWhenEmpty="true" OnRowDataBound = "RowDataBound_EstimateSubMaintenance">
                        <AlternatingRowStyle BackColor="White" />
                        <Columns>
                            <asp:TemplateField HeaderText="No">
                                <ItemTemplate><span id="dataItemIndex"><%# Container.DataItemIndex + 1 %></span></ItemTemplate>
                                <ItemStyle CssClass="data_AC" />
                                <HeaderStyle CssClass="cell_center" Width="5%" />
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="제품명">
                                <ItemTemplate>
                                    <asp:Label ID="PRODUCTNAME" runat="server" Text='<%# Eval("PRODUCTNAME") %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle CssClass="data_AL" />
                                <HeaderStyle Width="15%" />
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="상세내역">
                                <ItemTemplate>
                                    <div style="width: 225px;" title='<%# Eval("REMARK") %>'>
                                        <asp:Label ID="lblREMARK" runat="server" Text='<%# Eval("REMARK") %>'></asp:Label>
                                    </div>
                                </ItemTemplate>
                                <ItemStyle CssClass="data_AL" />
                                <HeaderStyle Width="29%" />
                            </asp:TemplateField>

                            <asp:TemplateField HeaderText="수량" HeaderStyle-CssClass="cell_center" HeaderStyle-Width="7%">
                                <ItemTemplate>
                                    <asp:Label ID="VOLUME" runat="server" Text='<%# SetComma(Eval("VOLUME")) %>'></asp:Label>
                                </ItemTemplate>
                                <ItemStyle CssClass="data_AR" />
                            </asp:TemplateField>
                        </Columns>
                        <HeaderStyle CssClass="cell_center" />
                        <FooterStyle BackColor="#507CD1" ForeColor="White" Font-Bold="True" />
                        <RowStyle />
                        <EmptyDataRowStyle CssClass="data_AC row25" />
                        <EmptyDataTemplate>
                            데이터가 존재하지 않습니다.
                        </EmptyDataTemplate>
                    </asp:GridView>
                </ContentTemplate>
            </asp:UpdatePanel>
        </asp:Panel>

 

 

==========================================================================

Estimate.aspx.cs => 코드

==========================================================================

#region PDF 버튼 클릭 이벤트
        protected void btnExport_Click(object sender, EventArgs e)
        {
            try
            {
                string fileName = "Estimate_Maintenance_PDF.pdf";
                string TargetFile = Request.PhysicalApplicationPath + "\\Pages__C013\\files\\" + fileName;

                // pdf 파일 저장
                GetDetail(TargetFile);

                Response.ContentType = "application/x-download";
                Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
                Response.WriteFile(TargetFile);
                Response.Flush();
                Response.End();
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
        #endregion


        #region html pdf 파일로 저장
        private void GetDetail(string TargetFile)
        {
            string strHtml = string.Empty;
            StringWriter sw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(sw);
            pnCerti.RenderControl(hw);
            StringReader sr = new StringReader(sw.ToString());
            strHtml = sr.ReadToEnd();
            sr.Close();

            var htmlContent = "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>"
                + "<link href='" + host + "/Pages__C013/Styles/AddStyle.css' rel='stylesheet'>"
                + "<link href='" + host + "/Styles/CommonStyle.css' rel='stylesheet'>"
                + "<link href='" + host + "/Styles/layout.css' rel='stylesheet' type='text/css'>"
                + "<link href='" + host + "/Styles/topnavi.css' rel='stylesheet' type='text/css'>"
                + "<link href='" + host + "/Styles/topnavi_tenant.css' rel='stylesheet' type='text/css'>"
                + "<link href='" + host + "/Styles/common.css' rel='stylesheet' type='text/css'>"
                + "<body>" + strHtml + "</body>";

            var htmlToPdf = new NReco.PdfGenerator.HtmlToPdfConverter();
            var pdfBytes = htmlToPdf.GeneratePdf(htmlContent);


            string ModifiedFileName = string.Empty;
            string FinalFileName = string.Empty;

            File.WriteAllBytes(TargetFile, pdfBytes);
        }

        public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
        {
              // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.
        }
        #endregion

 

 

*** 겁나 삽질 끝에 찾은 NReco.PdfGenerator

*** 아주~ 잘된다~

 

 

나와 같이 삽질하는 사람들을 위해서 공유 드립니다.

 

NReco.PdfGenerator.dll


'IT > C#' 카테고리의 다른 글

c# 숫자및 소수점 입력 onkeypress  (0) 2018.01.02
숫자만 입력하게하기  (0) 2017.12.18
로딩바 화면 구현  (0) 2017.01.17
csv 만들기  (0) 2016.12.27
그리드뷰(GridView) 필드 동적 생성  (0) 2015.06.22

+ Recent posts