locked
Converting panel to pdf RRS feed

  • Question

  • User661479005 posted

    I have some information in a panel that I like to convert to pdf.

    This is my code:

    Sub btnExport_Click(sender As Object, e As EventArgs)
    	Dim attachment As String = "attachment; filename=Article.pdf"
    	Response.ClearContent()
    	Response.AddHeader("content-disposition", attachment)
    	Response.ContentType = "application/pdf"
    	Dim stw As New StringWriter()
    	Dim htextw As New HtmlTextWriter(stw)
    	panelPdf.RenderControl(htextw)
    	Dim document As New Document()
    	PdfWriter.GetInstance(document, Response.OutputStream)
    	document.Open()
    	Dim str As New StringReader(stw.ToString())
    	Dim htmlworker As New HTMLWorker(document)
    	htmlworker.Parse(str)
    	document.Close()
    	Response.Write(document)
    	Response.[End]()
    End Sub
    I got this error which I dont understand because I didnt add an url:
    Line 127: htmlworker.Parse(str) 
    Exception Details: System.IO.IOException: The network path was not found.

    Friday, August 19, 2011 7:46 AM

All replies

  • User1100692814 posted

    Hi

    I have used iTextSharp for creating pdf's in the past. It's free and quite simple to use. Check out http://www.codeproject.com/KB/graphics/iTextSharpTutorial.aspx for details.

    Hope it helps.

    D

    Friday, August 19, 2011 8:11 AM
  • User661479005 posted

    Hi Dave

    I am already using ItextSharp in my code.

    I found an article how to make pdf html in a panel or div on the page:
    http://www.codedigest.com/Articles/ASPNET/344_Export_to_PDF_file_in_ASPNet-Gridview_to_PDF_ASPX_Page_Content_to_PDF.aspx 

    I have created pdf from datagrid succesfully, but my problem comes when I try to make a pdf from a panel

    Here is some more of my  code:

    <%@ import namespace="System.Data "%>
    <%@ import namespace="System.Data.odbc "%>
    <%@ import namespace="System.Collections"%>
    <%@ import namespace="System.Configuration"%>
    <%@ import namespace="System.Linq"%>
    <%@ import namespace="System.Web.Security"%>
    <%@ import namespace="System.IO"%>
    <%@ import namespace="System.Xml"%>
    <%@ import namespace="System.Text"%>
    <%@ import namespace="System.Xml.Linq"%>
    <%@ import namespace="iTextSharp.text"%>
    <%@ import namespace="iTextSharp.text.pdf"%>
    <%@ import namespace="iTextSharp.text.html"%>
    <%@ import namespace="iTextSharp.text.html.simpleparser"%>
    <%@ Import Namespace="functionLibrary" %>
    
    Sub btnExport_Click(sender As Object, e As EventArgs)
    	Dim attachment As String = "attachment; filename=Article.pdf"
    	Response.ClearContent()
    	Response.AddHeader("content-disposition", attachment)
    	Response.ContentType = "application/pdf"
    	Dim stw As New StringWriter()
    	Dim htextw As New HtmlTextWriter(stw)
    	panelPdf.RenderControl(htextw)
    	Dim document As New Document()
    	PdfWriter.GetInstance(document, Response.OutputStream)
    	document.Open()
    	Dim str As New StringReader(stw.ToString())
    	Dim htmlworker As New HTMLWorker(document)
    	htmlworker.Parse(str)
    	document.Close()
    	Response.Write(document)
    	Response.[End]()
    End Sub
      
    
    <asp:Panel runat="server" ID="panelPdf" >
            <table border="0" width="100%" cellpadding="0" cellspacing="0" >
            <tr>
            <td align="right" runat="server" id="tdPostHeader" class="postheader_laes">
                <table width="100%" border="0" cellspacing="0" cellpadding="0" class="nyhedsbrevheader_laes">
                <tr>
                <td align="left" style="text-indent:10px;" height="25">
                <asp:HyperLink runat="server" ID="hlVedhaeftet" NavigateUrl="#vedhaeftet" Text='<img src="/images/clips.png" border="0"' /></td>
                <td><asp:Label runat="server" ID="lblOverskrift" CssClass="standardTextBold" /></td>
                <td><asp:Label runat="server" ID="lblAfsender" CssClass="adminTextKursiv" /></td>
                </tr>
                </table>
            </td>
            </tr>
            <tr>
            <td class="postborder_laes">
               <table width="98%" border="0" cellspacing="0" cellpadding="4" align="center">
               <tr>
               <td><asp:Label runat="server" ID="lblNyhedsbrev" CssClass="adminText" /></td>
               </tr>
               </table>
            </td>
            </tr>
            </table>
       </asp:Panel>
    
    Friday, August 19, 2011 8:28 AM
  • User-1842880510 posted
    Did you ever get an answer to this issue. I am experiencing the same thing.
    Friday, August 23, 2013 4:40 PM
  • User661479005 posted

    Hi joeller

    Yes - I got it work like that:

    Here is part of the file where the pdf is

    Sub prepare_pdf()
    	Dim sb As New StringBuilder()
    	Dim tw As New StringWriter(sb)
    	Dim hw As New HtmlTextWriter(tw)
    	panelPDF.RenderControl(hw)
    	Dim htmlDisplayText As String = sb.ToString()
    	session("pdfpanel") = htmlDisplayText
    	session("rotatepdf") = False
    	session("pdflinebreak") = False
    	imgbPdf.Attributes.add("onclick","var w=window.open('/windows/pdfpanel.aspx?doc=logininformation.pdf','panelpdf','width=700,height=800');w.focus();return false;")
    End Sub
    
                <asp:Panel runat="server" ID="panelPdf" Visible="false" >
                   <asp:Label runat="server" ID="lblMyPdfInformation" />
                </asp:Panel>
    

    This is the file that create the pdf:

    <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="utf-8" %>
    <%@ import namespace="System.Data "%>
    <%@ import namespace="System.Data.sql "%>
    <%@ import namespace="MySql.Data.MySqlClient "%>
    <%@ import namespace="System.Collections"%>
    <%@ import namespace="System.Configuration"%>
    <%@ import namespace="System.Linq"%>
    <%@ import namespace="System.Web.Security"%>
    <%@ import namespace="System.IO"%>
    <%@ import namespace="System.Xml"%>
    <%@ import namespace="System.Text"%>
    <%@ import namespace="System.Xml.Linq"%>
    <%@ import namespace="iTextSharp.text"%>
    <%@ import namespace="iTextSharp.text.pdf"%>
    <%@ import namespace="iTextSharp.text.html"%>
    <%@ import namespace="iTextSharp.text.html.simpleparser"%>
    <%@ Import Namespace="functionLibrary" %>
    
    <!--#include virtual="/includes/alert.aspx" -->
    
    <script runat="server">
    Dim verdanaHeader As Font
    
    Sub Page_Load(Src As Object, E As EventArgs)
      if fl.authpop() then
         verdanaHeader = FontFactory.GetFont("Verdana", 11)
         open_pdf()
      End if
    End Sub
    
    
    Sub open_pdf()
    	 Dim document As Document
    	 If session("rotatepdf") = True Then
    		document = New Document(PageSize.A4.Rotate(), 30.0F, 10.0F, 10.0F, 10.0F)
    	 Else
    		document = New Document(PageSize.A4, 50.0F, 10.0F, 10.0F, 10.0F)
    	 End if
    
    '	Dim document As New Document(PageSize.A4, 50.0F, 10.0F, 10.0F, 10.0F)
    	Dim ms As New MemoryStream()
    	Dim writer As PdfWriter = PdfWriter.GetInstance(document, ms)
    	Dim strHTML as string = session("pdfpanel")
    	Dim arrAdresser as arrayList
    	
    	strHTML = change_html(strHTML,"<img"," src=""",true)
    	strHTML = change_html(strHTML,"<a"," href=""",false)
    	
    	document.Open()
    	Try
    	 ' se om der skal adresser på
    	   If session("pdfadressestring") <> ""
    	      arrAdresser = new arrayList(fl.stringToArray(session("pdfadressestring"),"|"))
    	      For i = 0 to arrAdresser.count -1
    	         Dim se As New StringReader(strHTML)
    	         Dim obj As New HTMLWorker(document)
    			 Dim strChunk as string = vbcrlf & vbcrlf & vbcrlf & vbcrlf & arrAdresser(i).ToString & vbcrlf & vbcrlf
    			 If i = 0 Then strChunk = vbcrlf & strChunk
                 Dim myAddress as new Chunk(strChunk, verdanaHeader)
                 Dim pAddress as new Phrase(myAddress)
                 document.Add(pAddress)
    	         obj.Parse(se)
    		     document.NewPage()
    	      Next
           Else
    	     If session("pdflinebreak") = True Then
    	         Dim strLinebreak() as string = split(strHTML,"pdflinebreak")
    			 For i = 0 to Ubound(strLinebreak)
    				  Dim se As New StringReader(strLinebreak(i))
    				  Dim obj As New HTMLWorker(document)
    				  obj.Parse(se)
    		          document.NewPage()
    			 Next
    		 Else
    	        Dim se As New StringReader(strHTML)
    	        Dim obj As New HTMLWorker(document)
    	        obj.Parse(se)
    		 End if
           End if
    	   document.Close()
     	   Response.Clear()
     	   Response.AddHeader("content-disposition", "attachment; filename=" & request.QueryString("doc"))
     	   Response.ContentType = "application/pdf"
     	   Response.Buffer = True
     	   Response.OutputStream.Write(ms.GetBuffer(), 0, ms.GetBuffer().Length)
    	Catch ex as exception ' hvis der er fejl  vis html-siden istedet for
        	response.write(strHTML)
    
    	Finally
     	   Response.End() 
    	End Try
    	session("pdfadressestring") = ""
    End Sub
    
    
    
    Function change_html(strHTML as string,strTag1 as string,strTag2 as string,blnTag as boolean) as string
    	Dim strNewHTML as string = ""
    	Dim splitHTML() as string = split(strHTML,strTag1)
    	If Ubound(splitHTML) > 0 Then
    	   Dim strPath,strNewPath as string
    	   Dim lenHTML,lenFile,lenTag as integer
    	   For i as integer = 0 to Ubound(splitHTML) -1
    	      If i=0 Then strNewHTML += splitHTML(i)
    		  lenHTML = len(splitHTML(i+1))
    	      Dim splitFile() as string = split(splitHTML(i+1),">")
    	      If Ubound(splitFile) > 0 Then
    		     lenFile = len(splitFile(0))
    			 lenTag = len(strTag1) + lenFile + 1  ' 1 er dette sluttag >
    	         Dim splitSrc() as string = split(splitFile(0),strTag2)
    		     If Ubound(splitSrc) > 0 Then
    	            Dim splitPath() as string = split(splitSrc(1),"""")
    			    If Ubound(splitPath) > 0 Then
    	               strPath = splitPath(0)
    			    End if
    		     End if
    	      End if
    	      If strPath <> "" Then 
    		     Try
    		        strNewPath = server.MapPath(Replace(strPath,fl.fullSystemURL,""))
    			 Catch ' går i fejl hvis det ikke er en virtual path
    			 end Try
    			 'indsæt <br /> inden tag idet billede altid ligger for højt og næsten overskriver den sidste tekst
    			 If blnTag = true Then ' hvis der ønskes tag for denne control f.eks. images
    		        strNewHTML += "<br />" & strTag1 & replace(splitFile(0),strPath,strNewPath) & ">" & Right(splitHTML(i+1),(lenHTML + len(strTag1))-lenTag)
    			 Else ' hvis der ikke ønskes tag som f.eks.link
    		        strNewHTML += Replace( Right(splitHTML(i+1),lenHTML-(lenFile +1)) ,"</a>","")
    			 End if
    		  Else strNewHTML = splitHTML(i) & strTag1 & splitHTML(i+1)
    	      End if
    	   Next
    	Else strNewHTML = strHTML
    	End if
    	return strNewHTML
    End Function
    
    
    </script>
    
    
    
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>printervenlig PDF</title>
    </head>
    <body>
    </body>
    </html>
    <script>
      <asp:Literal id="ltlAlert" runat="server"
                   EnableViewState="False">
        </asp:Literal>
      </script>
    




    Hope it will help

    Saturday, August 24, 2013 3:24 AM