将iTextSharp从HTMLWorker更改为XMLWorker

时间:2015-12-27 19:01:08

标签: html vb.net itext

我有一个工作程序,可以使用iTextSharp中的HTMLWorker从页面的HTML中生成PDF。我想正确显示我的CSS,所以我要转换到更新版本的iTextSharp XML工具。

我遇到的问题是,当生成PDF时,它会遗漏一堆页面。它获取页面的顶部,但页面的其余部分根本不会生成,尽管它是正在使用的HTML字符串的一部分。 IE:我可以调试和查看字符串内容,剩下的东西在字符串中。

想法?

'this is the section that used to work
    '---------------------------------------------------------------------------

    'Create a pdf in a memorystream
    Dim ms As New MemoryStream
    Dim doc As New Document(PageSize.A4)
    Dim writer As PdfWriter = PdfWriter.GetInstance(pdfDocument, pdfMemoryStream)
    'Open the pdf
    pdfDocument.Open()
    'Reads the HTML to pdf
    Dim htmlRenderString As String = Session("htmlRender").ToString
    Dim reader As New StringReader(htmlRenderString)
    Dim obj As New simpleparser.HTMLWorker(doc)
    If Not (reader Is Nothing) Then
        obj.Parse(reader)
    End If

    'Close the pdf, but leave the Stream open and set the Stream position to 0
    pdfWrite.CloseStream = False
    pdfDocument.Close()
    pdfMemoryStream.Position = 0

    '---------------------------------------------------------------------------

    Dim pdfMemoryStream As New MemoryStream
    'this document sets the sheet size
    Dim pdfDocument As New Document(PageSize.A4)
    'the writer uses the memory stream and puts it in the document
    Dim pdfWrite As PdfWriter = PdfWriter.GetInstance(pdfDocument, pdfMemoryStream)
    'Open the pdf
    pdfDocument.Open()
    'Reads the HTML to pdf

    Dim htmlRenderString As String = Session("htmlRender").ToString
    Dim reader As New StringReader(htmlRenderString)
    XMLWorkerHelper.GetInstance().ParseXHtml(pdfWrite, pdfDocument, reader)

    'Close the pdf, but leave the Stream open and set the Stream position to 0
    pdfWrite.CloseStream = False
    pdfDocument.Close()
    pdfMemoryStream.Position = 0

    'Response.ContentType = "application/pdf"
    'Response.TransmitFile()
    Response.Clear()
    Dim bytes As Byte() = pdfMemoryStream.GetBuffer()
    Response.Buffer = True
    Response.Clear()
    Response.ContentType = "application/pdf"
    Response.AppendHeader("Content-Disposition", "filename=Quote.pdf")

    Response.BinaryWrite(bytes)
    Response.End()

以下是实时HTML:

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
  <title>

    Quote

  </title>
  <link href="../Content/Report.css" rel="stylesheet" type="text/css" />
  <script type="text/javascript" src="/9A944FE92F894808AA888B43A507B098/88AE1FCF-47E5-264D-A5C1-AD49936FA10E/main.js" charset="UTF-8"></script>
</head>

<body>

  <form method="post" action="./quoteReport.aspx?QuoteID=10688&amp;QuoteType=Windows" id="form1">

    <div class="aspNetHidden">

      <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUJLTI5Mzg2MjQ3D2QWAgIDD2QWDAIKDxYCHgRUZXh0BbcBPGI+PHU+UXVvdGUgRGF0ZTwvdT48L2I+PGJyIC8+MTIvMjIvMjAxNTxiciAvPk91ciBQTzoxMDY4ODxiciAvPjxiciAvPjxiPjx1PkN1c3RvbWVyPC91PjwvYj48YnIgLz5EYW4mbmJzcDtXaWVyPGJyIC8+dGVzdDxiciAvPiwgTUkgPGJyIC8+PGJyIC8+PGJyIC8+PGI+UXVvdGUgaXMgZ29vZCBmb3IgMzAgZGF5cy48L2I+ZAIPDzwrAAkBAA8WBB4IRGF0YUtleXMWAB4LXyFJdGVtQ291bnQCAmQWBGYPZBYCZg8VAQtXaW5kb3cgVGVzdGQCAQ9kFgJmDxUBDldpbmRvd3MgdGVzdCAyZAITDxYCHwAF4QI8dGFibGUgaWQ9J1BheW1lbnRUYWJsZScgc3R5bGU9J3dpZHRoOjcwMHB4Oyc+PHRyPjx0ZCBzdHlsZT0ndGV4dC1hbGlnbjpsZWZ0OyB3aWR0aDo0NTBweCc+VG90YWwgZm9yIGFsbCBtYXRlcmlhbCBhbmQgbGFib3I6PC90ZD48dGQ+JDEsMDAwLjAwPC90ZD48L3RyPjx0cj48dGQgc3R5bGU9J3RleHQtYWxpZ246bGVmdDsgd2lkdGg6NDUwcHgnPkxlc3MgZG93biBwYXltZW50OjwvdGQ+PHRkPjx1PiQwLjAwPC91PjwvdGQ+PC90cj48dHI+PHRkIHN0eWxlPSd0ZXh0LWFsaWduOmxlZnQ7IHdpZHRoOjQ1MHB4Jz5Ub3RhbCBkdWUgdXBvbiBjb21wbGV0aW9uOjwvdGQ+PHRkPiQxLDAwMC4wMDwvdGQ+PC90cj48L3RhYmxlPmQCGA9kFgICAQ88KwAJAQAPFgQfARYAHwICAWQWAmYPZBYCZg8VAg1UZXN0IG9wdGlvbiAxAzE1MGQCIg8PZA8QFgFmFgEWAh4OUGFyYW1ldGVyVmFsdWUFBTEwNjg4FgECBGRkAiUPD2QPEBYBZhYBFgIfAwUFMTA2ODgWAQIEZGRk3+RlBEo+bg/JeGNHanBJlif72Uwcn59illMDYvxopuE="
      />

    </div>

    <div>

      <input type="submit" name="downloadButton" value="Download" id="downloadButton" />

      <div id="Header" style="width: 100%; overflow: hidden;">

        <table style="width:700px;">

          <tr>

            <td>

              <img src="../Images/ebrl300.jpg" alt="ebrl" width="300" height="173" />

            </td>

            <td style="border:solid; text-align:center;">

              <input type="hidden" name="customerNameHiddenField" id="customerNameHiddenField" value="Dan Wier" />

              <b><u>Quote Date</u></b>
              <br />12/22/2015
              <br />Our PO:10688
              <br />
              <br /><b><u>Customer</u></b>
              <br />Dan&nbsp;Wier
              <br />test
              <br />, MI
              <br />
              <br />
              <br /><b>Quote is good for 30 days.</b>

            </td>

          </tr>

        </table>

      </div>

      <br /><b>We hereby propose to furnish and install:</b>

      <br />

      <br />

      <div id="QuoteItems" style="width: 700px; margin-bottom: 10px;">

        <div id="pageSizingDiv" style="min-height:660px; width: 700px;background-color:oldlace;">

          <div id="QuoteLineItems" style="margin-bottom: 10px;">

            <table id="quoteLineItemsDataList" cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">

              <tr>

                <td class="QuoteItem">

                  Window Test

                </td>

              </tr>
              <tr>

                <td class="QuoteItem">

                  Windows test 2

                </td>

              </tr>

            </table>

          </div>

          <br />

        </div>

        <div id="QuotePayment" style="height:80px;clear:both;margin-bottom: 10px;">

          <div id="QPFloatRight" style="text-align: right;float:right;">

            <table id='PaymentTable' style='width:700px;'>
              <tr>
                <td style='text-align:left; width:450px'>Total for all material and labor:</td>
                <td>$1,000.00</td>
              </tr>
              <tr>
                <td style='text-align:left; width:450px'>Less down payment:</td>
                <td><u>$0.00</u>
                </td>
              </tr>
              <tr>
                <td style='text-align:left; width:450px'>Total due upon completion:</td>
                <td>$1,000.00</td>
              </tr>
            </table>

          </div>

        </div>

        <br />

        <div id="OptionalPanel">

          <div id="QuoteOptionalServicesDiv" style="margin-bottom: 10px;width:700px;">

            <span style="margin-bottom: 30px;"><b>Optional Services:</b></span>

            <table id="optionalServicesDataList" cellspacing="0" style="border-collapse:collapse;">

              <tr>

                <td>

                  <table style="width:700px">

                    <tr style="margin-bottom: 10px; ">

                      <td style="text-align:left; width:450px"><span style="text-align:left;">Test option 1</span>
                      </td>

                      <td style="text-align:right;">150</td>

                      <td>
                        <img src="../Images/checkbox.png" alt="checkbox" />
                      </td>

                      <td>
                        <img src="../Images/checkbox.png" alt="checkbox" />
                      </td>

                    </tr>

                  </table>

                </td>

              </tr>

            </table>

          </div>

          <br />

        </div>

        <div style="border: solid thin; font-size: medium; margin-bottom: 10px;">

          nc. is a licensed maintenance and alteration contractor license # . Any additional work under the amount of $500.00 will be performed and added to the final bill. Any additional work over $500.00 will constitute the signing of a work change order by the
          homeowner and then added to the final bill. , will not be held responsible for any delay in the completion of the work resulting from strikes, fire, accidents, unavailability of materials, or other causes beyond our control. Brothers Roofing
          Inc. will not be held responsible for any damaged concrete, patios, plaster or drywall, or damage to interior of home of any kind from the installation or removal of building materials.

        </div>

        <br />

        <div id="freezingDiv" style="border: solid thin; font-size: medium; margin-bottom: 10px;">

          It is possible tha the original roofing shingles may have bonded to the original ice and water shield. It is policy to remove the plywood and replace it under such a circumstance at a cost of $60.00 pr sheet.

        </div>

        <br />

        <div>

          <b>ACCEPTANCE OF PROPOSAL:</b> The price above, specifications, and conditions are satisfactory and are hereby accepted. No additional discounts or coupons unless approved. You are authorized to do the work as specified. Payments to be made as
          indicated. In the event of default, I understand and agree that I am legally liable for 1-1/2% simple interest per month (18 % APR) and all costs of collection, including, collection agency fees, reasonable attorney fees, court costs, and all
          other costs to collect this debt. <span id="ccSpan" style="color: red;">A 3% convenience fee will be applied to all credit card sales.</span> This proposal may be withdrawn if not accepted within thirty days.

          <br />

          <p style="text-align: center;"><b>Balance due upon completion.<br />

A 2% Finance charge will be added to balance due

if not paid within 7 days of invoice date. </b>
          </p>

          <br />

          <br />Signature: __________________________________________________ Date: ______________

          <br />

          <div class="centerText">

            <span></span>

          </div>

        </div>

      </div>

    </div>

    <div class="aspNetHidden">

      <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="A981BE79" />

      <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAPTlquB+EQZPGV2FMqvSKL59CW6QoKEppMjLhO31cevKxfcIMTOGmnQTU/WjrS/9Rr4bBc25bPDIiSZdBLYnK2a4S+l6nk6g65UPVjREO5cCw==" />

    </div>
  </form>

</body>

</html>

以下是会话字符串中的HTML:

"" & vbCrLf & "" & vbCrLf & "<!DOCTYPE html>" & vbCrLf & "" & vbCrLf & "<html xmlns="http://www.w3.org/1999/xhtml">" & vbCrLf & "<head><title>" & vbCrLf & "    Quote" & vbCrLf & "</title><link href="../Content/Report.css" rel="stylesheet" type="text/css" /></head>" & vbCrLf & "<body>" & vbCrLf & "    <form method="post" action="./quoteReport.aspx?QuoteID=10688&amp;QuoteType=Windows" id="form1">" & vbCrLf & "<div class="aspNetHidden">" & vbCrLf & "" & vbCrLf & "</div>" & vbCrLf & "" & vbCrLf & "    <div>" & vbCrLf & "        " & vbCrLf & "        <input type="submit" name="downloadButton" value="Download" id="downloadButton" />" & vbCrLf & "        <div id="Header" style="width: 100%; overflow: hidden;">" & vbCrLf & "            " & vbCrLf & "" & vbCrLf & "        <table style="width:700px;">" & vbCrLf & "            <tr>" & vbCrLf & "                <td>" & vbCrLf & "                    <img src="../Images/ebrl300.jpg" alt="ebrl" width="300" height="173" />" & vbCrLf & "                </td>" & vbCrLf & "                <td style="border:solid; text-align:center;">" & vbCrLf & "                    <input type="hidden" name="customerNameHiddenField" id="customerNameHiddenField" value="Dan Wier" />" & vbCrLf & "                    <b><u>Quote Date</u></b><br />12/22/2015<br />Our PO:10688<br /><br /><b><u>Customer</u></b><br />Dan&nbsp;Wier<br />test<br />, MI <br /><br /><br /><b>Quote is good for 30 days.</b>" & vbCrLf & "" & vbCrLf & "                </td>" & vbCrLf & "            </tr>" & vbCrLf & "" & vbCrLf & "        </table>" & vbCrLf & "" & vbCrLf & "    </div> " & vbCrLf & "" & vbCrLf & "        <br /><b>We hereby propose to furnish and install:</b>" & vbCrLf & "        " & vbCrLf & "        <br />" & vbCrLf & "        <br />" & vbCrLf & "    " & vbCrLf & "    <div id="QuoteItems" style="width: 700px; margin-bottom: 10px;">" & vbCrLf & "        <div id="pageSizingDiv" style="min-height:660px; width: 700px;background-color:oldlace;" >" & vbCrLf & "" & vbCrLf & "            <div id="QuoteLineItems" style="margin-bottom: 10px;">" & vbCrLf & "                <table id="quoteLineItemsDataList" cellspacing="0" rules="all" border="1" style="border-collapse:collapse;">" & vbCrLf & "    <tr>" & vbCrLf & "        <td class="QuoteItem">" & vbCrLf & "                        Window Test" & vbCrLf & "                    </td>" & vbCrLf & "    </tr><tr>" & vbCrLf & "        <td class="QuoteItem">" & vbCrLf & "                        Windows test 2" & vbCrLf & "                    </td>" & vbCrLf & "    </tr>" & vbCrLf & "</table>" & vbCrLf & "            </div>" & vbCrLf & "" & vbCrLf & "            <br />" & vbCrLf & "            " & vbCrLf & "        </div>" & vbCrLf & "        " & vbCrLf & "        <div id="QuotePayment" style="height:80px;clear:both;margin-bottom: 10px;">" & vbCrLf & "            <div id="QPFloatRight" style="text-align: right;float:right;">" & vbCrLf & "                <table id='PaymentTable' style='width:700px;'><tr><td style='text-align:left; width:450px'>Total for all material and labor:</td><td>$1,000.00</td></tr><tr><td style='text-align:left; width:450px'>Less down payment:</td><td><u>$0.00</u></td></tr><tr><td style='text-align:left; width:450px'>Total due upon completion:</td><td>$1,000.00</td></tr></table>" & vbCrLf & "" & vbCrLf & "            </div>" & vbCrLf & "        </div> " & vbCrLf & "        <br />" & vbCrLf & "        <div id="OptionalPanel">" & vbCrLf & "    " & vbCrLf & "                <div id="QuoteOptionalServicesDiv" style="margin-bottom: 10px;width:700px;">" & vbCrLf & "" & vbCrLf & "                    <span style="margin-bottom: 30px;"><b>Optional Services:</b></span>" & vbCrLf & "                    <table id="optionalServicesDataList" cellspacing="0" style="border-collapse:collapse;">" & vbCrLf & "        <tr>" & vbCrLf & "            <td>        " & vbCrLf & "                            <table style="width:700px">                    " & vbCrLf & "                                    <tr style="margin-bottom: 10px; ">" & vbCrLf & "                                        <td style="text-align:left; width:450px"><span style="text-align:left;">Test option 1</span></td>" & vbCrLf & "                                        <td style="text-align:right;">150</td>" & vbCrLf & "                                        <td><img src="../Images/checkbox.png" alt="checkbox"/></td>" & vbCrLf & "                                        <td><img src="../Images/checkbox.png" alt="checkbox"/></td>" & vbCrLf & "                                    </tr>       " & vbCrLf & "                            </table>  " & vbCrLf & "                        </td>" & vbCrLf & "        </tr>" & vbCrLf & "    </table>" & vbCrLf & "" & vbCrLf & "                </div> " & vbCrLf & "            <br />" & vbCrLf & "        " & vbCrLf & "</div> " & vbCrLf & "" & vbCrLf & "        " & vbCrLf & "" & vbCrLf & "        <div style="border: solid thin; font-size: medium; margin-bottom: 10px;">" & vbCrLf & "                . is a licensed maintenance and alteration contractor license # .  Any additional work under the amount of $500.00 will be performed and added to the final bill.  Any additional work over $500.00 will constitute the signing of a work change order by the homeowner and then added to the final bill.  , will not be held responsible for any delay in the completion of the work resulting from strikes, fire, accidents, unavailability of materials, or other causes beyond our control.   will not be held responsible for any damaged  concrete, patios,  plaster or drywall, or damage to interior of home of any kind from the installation or removal of building materials.  " & vbCrLf & "" & vbCrLf & "        </div> " & vbCrLf & "        <br />" & vbCrLf & "        <div id="freezingDiv" style="border: solid thin; font-size: medium; margin-bottom: 10px;">" & vbCrLf & "                It is possible tha the original roofing shingles may have bonded to the original ice and water shield. It is  policy to remove the plywood and replace it under such a circumstance at a cost of $60.00 pr sheet." & vbCrLf & "" & vbCrLf & "        </div> " & vbCrLf & "" & vbCrLf & "        <br />" & vbCrLf & "        <div >" & vbCrLf & "            <b>ACCEPTANCE OF PROPOSAL:</b>  The price above, specifications, and conditions are satisfactory and are hereby accepted.   No additional discounts or coupons unless approved.  You are authorized to do the work as specified.  Payments to be made as indicated.  In the event of default, I understand and agree that I am legally liable for 1-1/2% simple interest per month (18 % APR) and all costs of collection, including, collection agency fees, reasonable attorney fees, court costs, and all other costs to collect this debt.  <span id="ccSpan" style="color: red;">A 3% convenience fee will be applied to all credit card sales.</span> This proposal may be withdrawn if not accepted within thirty days.     " & vbCrLf & "            <br />" & vbCrLf & "            <p style="text-align: center;"><b>Balance due upon completion.<br />" & vbCrLf & "A 2% Finance charge will be added to balance due " & vbCrLf & "if not paid within 7 days of invoice date. </b></p>" & vbCrLf & "            <br />" & vbCrLf & "            <br />" & vbCrLf & "            Signature: __________________________________________________" & vbCrLf & "            Date: ______________" & vbCrLf & "            <br />" & vbCrLf & "            <div class="centerText">" & vbCrLf & "            <span >Address</span>" & vbCrLf & "            </div>" & vbCrLf & "        </div> " & vbCrLf & "        </div>" & vbCrLf & "    </div> " & vbCrLf & "        " & vbCrLf & "    " & vbCrLf & "" & vbCrLf & "" & vbCrLf & "" & vbCrLf & "    " & vbCrLf & "    " & vbCrLf & "<div class="aspNetHidden">" & vbCrLf & "" & vbCrLf & "    <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="A981BE79" />" & vbCrLf & "    <input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEdAAPTlquB+EQZPGV2FMqvSKL59CW6QoKEppMjLhO31cevKxfcIMTOGmnQTU/WjrS/9Rr4bBc25bPDIiSZdBLYnK2a4S+l6nk6g65UPVjREO5cCw==" />" & vbCrLf & "</div></form>" & vbCrLf & "</body>" & vbCrLf & "</html>" & vbCrLf & ""

会话字符串代码由此生成:

    Protected Overrides Sub Render(writer As HtmlTextWriter)
    ' setup a TextWriter to capture the markup
    ' http://stackoverflow.com/questions/1588431/how-get-html-of-current-page

    Dim tw As System.IO.TextWriter = New System.IO.StringWriter()
    Dim htw As New HtmlTextWriter(tw)

    ' render the markup into our surrogate TextWriter
    MyBase.Render(htw)

    ' get the captured markup as a string
    Dim pageSource As String = tw.ToString()

    ' render the markup into the output stream verbatim
    writer.Write(pageSource)

    ' remove the viewstate field from the captured markup

    viewStateRemoved = Regex.Replace(pageSource, "<input type=""hidden"" name=""__VIEWSTATE"" id=""__VIEWSTATE"" value="".*?"" />", "", RegexOptions.IgnoreCase)

    'also remove the success / failure spans
    '<span id="emailFailSpan" runat="server" visible="false" style="color:red;"></span>
    Dim startIndexInteger As Integer = viewStateRemoved.IndexOf("<span id=" & ControlChars.Quote & "email")
    Dim endIndexInteger As Integer = viewStateRemoved.IndexOf(".</span>") + 8

    If endIndexInteger > startIndexInteger And startIndexInteger > 0 Then
        Dim lengthInteger As Integer = endIndexInteger - startIndexInteger
        Dim spanSubString As String = viewStateRemoved.Substring(startIndexInteger, lengthInteger)
        viewStateRemoved = Regex.Replace(viewStateRemoved, spanSubString, "", RegexOptions.IgnoreCase)
    End If

    Session("htmlRender") = viewStateRemoved

End Sub

0 个答案:

没有答案