2015-06-26 86 views
0

我用itextsharp將HTML轉換爲PDF,我已經改進了itextsharp方面的代碼,在此線程之後iTextSharp XMLWorker parsing really slow 但現在我需要在html中進行改進。我的目標是達到時間低於0,1秒,我現在得到0,4秒改善html以提高itextsharp的性能

我不知道在itextsharp最快渲染的html端的規則是什麼。在我的HTML模板基本上我有風格的行內HTML和所有與表上完成,在這裏,我讓你的代碼

<html> 
 
    <head> 
 
    <title>DOCUMENT</title> 
 
    </head> 
 
    <body> 
 

 
    <!-- 2 --> 
 
    <table width="100%" cellspacing="0px" cellpadding="0px"> 
 
     <tr> 
 
     <!-- DETALLE DE LECTURA/CONSUMO OK--> 
 
     <td width="60%" style="padding-right:5px" valign="top"> 
 
      <table width="100%" style="font-size:8pt;" cellspacing="0px" cellpadding="0px"> 
 
      <tr> 
 
       <td width="100%" style="border:1px solid #2F3640;background-color:#2F3640;color:#FFFFFF;font-weight:bold;padding: 2px 5px;font-size:10pt;">DETALLE DE LECTURA/CONSUMO</td> 
 
      </tr> 
 
      <tr> 
 
        <td> 
 
        <table width="100%" style="font-size:8pt;text-align:center" cellspacing="0px" cellpadding="0px" border="0"> 
 
         <tr> 
 
         <td>Consumo &Uacute;ltimos 13 Meses (KWHs)</td> 
 
         </tr> 
 
         <tr> 
 
         <td width="100%"> 
 
          <!-- GRAFICO DE CONSUMO --> 
 
          <table width="100%" cellspacing="0" cellpadding="0" border="0"> 
 
          <!--BARRAS--> 
 
          <tr> 
 
           <td width="100%"> 
 
           <table width="100%" style="" cellspacing="0" cellpadding="0" align="left"> 
 
            <tr style="font-size:5pt;height:130px;"> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:4%"></td> 
 
             <!-- MES 1 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-1]</p> 
 
             <div style="width:100%; height:[valor-grafico-1-px]px; background-color:black"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 2 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-2]</p> 
 
             <div style="width:100%; height:[valor-grafico-2-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 3 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-3]</p> 
 
             <div style="width:100%; height:[valor-grafico-3-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 4 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-4]</p> 
 
             <div style="width:100%; height:[valor-grafico-4-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 5 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-5]</p> 
 
             <div style="width:100%; height:[valor-grafico-5-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 6 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-6]</p> 
 
             <div style="width:100%; height:[valor-grafico-6-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 7 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-7]</p> 
 
             <div style="width:100%; height:[valor-grafico-7-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 8 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-8]</p> 
 
             <div style="width:100%; height:[valor-grafico-8-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 9 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-9]</p> 
 
             <div style="width:100%; height:[valor-grafico-9-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 10 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-10]</p> 
 
             <div style="width:100%; height:[valor-grafico-10-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 11 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-11]</p> 
 
             <div style="width:100%; height:[valor-grafico-11-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 12 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-12]</p> 
 
             <div style="width:100%; height:[valor-grafico-12-px]px; background-color:gray"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:2%"></td> 
 
             <!-- MES 13 --> 
 
             <td valign="bottom" style="font-size:5pt;width:5%"> 
 
             <p valign="center" style="width:100%; height:8px;">[valor-grafico-13]</p> 
 
             <div style="width:100%; height:[valor-grafico-13-px]px; background-color:black"> 
 
             </div> 
 
             </td> 
 
             <!-- --> 
 
             <td style="font-size:5pt;width:4%"></td> 
 

 
            </tr> 
 
           </table> 
 
           </td> 
 
          </tr> 
 
          <!-- EJE --> 
 
          <tr style="background-color:black; padding: 0; height: 1px"> 
 
           <td colspan="26" nowrap="" > 
 
           <div></div> 
 
           </td> 
 
          </tr> 
 
          <!-- MESES --> 
 
          <tr> 
 
           <td width="100%"> 
 
           <table width="100%" cellspacing="0" cellpadding="0"> 
 
            <tr> 
 
            <td style="font-size:5pt;" width="4%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-1]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-2]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-3]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-4]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-5]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-6]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-7]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-8]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-9]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-10]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-11]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-12]</td> 
 
            <td style="font-size:5pt;" width="2%"></td> 
 
            <td style="font-size:5pt;" width="5%">[mes-13]</td> 
 
            <td style="font-size:5pt;" width="4%"></td> 
 
            </tr> 
 
           </table> 
 
           </td> 
 
          </tr> 
 
          </table> 
 
         </td>  
 
         </tr> 
 
        </table> 
 
        </td> 
 
       </tr> 
 
       <tr> 
 
        <td width="100%" style="font-size:8pt;"> 
 
        <table width="100%" style="font-size:8pt;" cellspacing="0px" cellpadding="0px"> 
 
         <tr> 
 
         <td width="50%">Datos consumo desde: [fecha-inicio-consumo]</td> 
 
         <td width="50%">hasta: [fecha-hasta-consumo]</td> 
 
         </tr> 
 
         <tr> 
 
         <td colspan="2" > 
 
          <table width="100%" style="font-size:10pt;text-align:center;" cellspacing="0px" cellpadding="0px"> 
 
          <tr> 
 
           <th>Medidores</th> 
 
           <th>Lectura Actual</th> 
 
           <th>Lectura Anterior</th> 
 
           <th>Diferencia</th> 
 
           <th>Constante</th> 
 
           <th>KWH</th> 
 
          </tr> 
 
          <tr> 
 
           <td>[medidor-1]</td> 
 
           <td>[lectura-actual-1]</td> 
 
           <td>[lectura-anterior-1]</td> 
 
           <td>[diferencia-1]</td> 
 
           <td>[constante-1]</td> 
 
           <td>[kwh-1]</td> 
 
          </tr> 
 
          <tr> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
          </tr> 
 
          <tr> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
           <td>[XX]</td> 
 
          </tr> 
 
          </table> 
 
         </td> 
 
         </tr> 
 
         <tr> 
 
         <td style="padding-top:5px" colspan="2"> 
 
          <table width="100%" style="font-size:8pt;text-align:right" cellspacing="0px" cellpadding="0px"> 
 
          <tr> 
 
           <td width="80%" style="font-size:10pt;">Consumo Energia(KWH/mensual)</td> 
 
           <td width="20%" style="font-size:10pt;">[consumo-energia]</td> 
 
          </tr> 
 
          <tr> 
 
           <td width="80%" style="font-size:10pt;">Consumo Energia Invierno(KWH/mensual)</td> 
 
           <td width="20%" ="font-size:10pt;">[consumo-energia-invierno]</td> 
 
          </tr> 
 
          <tr> 
 
           <td width="80%">Potencia Contratada o Le&iacute;da (KW)</td> 
 
           <td>[potencia-contratada]</td> 
 
          </tr> 
 
          <tr> 
 
           <td width="80%">Potencia Contratada o Le&iacute;da Horas de Punta(KW)</td> 
 
           <td>[potencia-hora-punta]</td> 
 
          </tr> 
 
          </table>    
 
         </td> 
 
         </tr> 
 
        </table> 
 
        </td> 
 
       </tr> 
 
       </table> 
 
     </td> 
 
     <td width="40%"> 
 
      <table width="100%" style="font-size:8pt;" cellspacing="0px" cellpadding="0px"> 
 
      <tr> 
 
       <td align="center"> 
 
       <img width="180px" src="http://localhost/FE.images/SEC.jpg" alt="sec"/> 
 
       </td> 
 
      </tr> 
 
      <tr> 
 
       <td> 
 
       <!-- ACUSE DE RECIBO --> 
 
       <table width="100%" style="font-size:8pt;"> 
 
        <tr> 
 
        <td width="40%">Recinto:</td> 
 
        <td width="60%" style="border-bottom:1px solid black;"></td> 
 
        </tr> 
 
        <tr> 
 
        <td>Nombre:</td> 
 
        <td width="60%" style="border-bottom:1px solid black;"></td> 
 
        </tr> 
 
        <tr> 
 
        <td>Fecha:</td> 
 
        <td width="60%" style="border-bottom:1px solid black;"></td> 
 
        </tr> 
 
        <tr> 
 
        <td>RUT:</td> 
 
        <td width="60%" style="border-bottom:1px solid black;"></td> 
 
        </tr> 
 
        <tr> 
 
        <td colspan="2" style="border-bottom:1px solid black;"> 
 
         <br/> 
 
        </td> 
 
        </tr> 
 
        <tr> 
 
        <td colspan="2" style="text-align:center">FIRMA</td> 
 
        </tr> 
 
       </table> 
 
       </td> 
 
      </tr> 
 
      </table> 
 
     </td> 
 
     </tr> 
 
    </table> 
 

 
    <!-- 3 --> 
 
    <table width="100%" style="font-size:8pt;" cellspacing="0px" cellpadding="0px"> 
 
     <tr> 
 
     <td width="65%"> 
 
      <table width="100%" style="padding-right:5px; font-size:8pt;" cellspacing="0px" cellpadding="0px"> 
 
      <tr> 
 
       <td style="border:1px solid #2F3640;background-color:#2F3640;color:#FFFFFF;font-weight:bold;padding: 2px 5px;font-size:10pt;"> 
 
       DETALLE DE LA CUENTA FACTURACION 
 
       </td> 
 
      </tr> 
 
      <tr> 
 
       <td> 
 
       <table width="100%" style="font-size:8pt;line-height:9pt;" cellspacing="0px" cellpadding="0px"> 
 
        <tr> 
 
        <td width="70%" style="font-size:10pt;" >Cargo fijo mensual</td> 
 
        <td width="30%" style="text-align:right;font-size:10pt;">$[cargo-fijo-mensual]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">Cargo &uacute;nico del sistema troncal ([cantidad-energia] kWh)</td> 
 
        <td style="text-align:right;font-size:10pt;">$[cargo-unico]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">Cargo por energ&iacute;a base ([cantidad-energia] kWh)</td> 
 
        <td style="text-align:right;font-size:10pt;">$[cargo-energia-base]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">Cargo por energ&iacute;a adicional de invierno ([cantidad-energia] kWh)</td> 
 
        <td style="text-align:right;font-size:10pt;">$[cargo-energia-invierno]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="border-top:1px solid black;font-size:10pt;" width="70%">Reliquidaciones</td> 
 
        <td style="border-top:1px solid black;text-align:right;font-size:10pt;" width="30%">$[reliquidaciones]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">Calibracion de medidor</td> 
 
        <td style="text-align:right;font-size:10pt;">$[calibracion-medidor]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">Inter&eacute;s por mora</td> 
 
        <td style="text-align:right;font-size:10pt;">$[interes-mora]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">Compensaciones</td> 
 
        <td style="text-align:right;font-size:10pt;">$[compensaciones]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-1]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-2]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-3]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-4]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-5]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-6]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-7]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-8]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-9]</td> 
 
        </tr> 
 
        <tr> 
 
        <td style="font-size:10pt;">item</td> 
 
        <td style="text-align:right;font-size:10pt;">$[item-10]</td> 
 
        </tr> 
 
        <tr> 
 
        <td width="70%" style="border-top:1px solid black;padding-top:5px;font-size:10pt;">Total:</td> 
 
        <td width="30%" style="border-top:1px solid black;padding-top:5px;text-align:right;font-size:10pt;">$[total-pagar]</td> 
 
        </tr> 
 
       </table> 
 
       </td> 
 
      </tr> 
 
      </table> 
 
     </td> 
 
     <!-- TOTALES --> 
 
     <td width="35%"> 
 
      <table width="100%" style="font-size:8pt;" cellspacing="0px" cellpadding="0px"> 
 
      <tr> 
 
       <td width="100%" align="left"> 
 
       <table width="95%" style="font-size:8pt;border:2px solid black;" cellspacing="0px" cellpadding="0px"> 
 
        <tr> 
 
        <th style="border-bottom:1px solid black;">DETALLE DE ABONOS</th> 
 
        </tr> 
 
        <tr> 
 
        <td>Total Abonados:</td> 
 
        <td></td> 
 
        </tr> 
 
        <tr> 
 
        <td>Fecha Abono:</td> 
 
        <td></td> 
 
        </tr> 
 
        <tr> 
 
        <td>Comprobante Caja &#8470;:</td> 
 
        <td></td> 
 
        </tr> 
 
       </table> 
 
       </td> 
 
      </tr> 
 
      <tr> 
 
       <td width="100%"> 
 
       <table width="100%" style="font-size:8pt;" cellspacing="0px" cellpadding="0px"> 
 
        <tr> 
 

 

 
        <td width="50%" style="font-size:10pt;">Fecha vencimiento:</td> 
 
        <td width="50%" style="font-size:10pt;">[fecha-vencimiento]</td> 
 
        </tr> 
 
        <tr> 
 
        <td width="50%" style="font-size:10pt;">Fecha de corte de suministro:</td> 
 
        <td width="50%" style="font-size:10pt;">[fecha-corte]</td> 
 
        </tr> 
 
        <tr> 
 
        <td width="50%" style="font-size:10pt;">&Uacute;ltimo pago realizado:</td> 
 
        <td width="50%" style="font-size:10pt;">[fecha-ultimo-pago]</td> 
 
        </tr> 
 
        <tr> 
 
        <table width="100%" style="background-color:black;color:white;font-weight:bold;font-size:10pt;" cellspacing="0px" cellpadding="0px"> 
 
         <tr> 
 
         <td width="50%" >TOTAL:</td> 
 
         <td width="50%" style="background-color:black;color:white;font-weight:bold;font-size:10pt;";>[total-pagar]</td> 
 
         </tr> 
 
        </table> 
 
        </tr> 
 
        <tr> 
 
        <td align="center"> 
 
         <img width="280px" src="#" alt="timbre"/> 
 
        </td> 
 
        </tr> 
 
       </table> 
 
       </td> 
 
      </tr> 
 
      </table>   
 
     </td> 
 
     </tr> 
 
    </table> 
 

 
    </body> 
 
</html>

回答

1

如果這已經在最近得到了解決,我不知道版本,但是從5.5.5開始,有關ImageProvider的問題。如果您未明確設置ImageProvider iTextSharp會在內部拋出異常,請以您的名義抓住它並創建一個ImageProvider。不幸的是,這可能會發生幾十次或幾百次,具體取決於您處理的HTML數量。

快速解決方法是明確設置您自己的ImageProvider。這可以只是一個簡單的所謂的「空白圖像提供商」shown here或者你可以做一個更復雜的。

這裏有一個VB.Net版本:

Public Class BlankImageProvider 
    Implements IImageProvider 

    Public Function GetImageRootPath() As String Implements IImageProvider.GetImageRootPath 
     Return "" 
    End Function 

    Public Sub Reset() Implements IImageProvider.Reset 

    End Sub 

    Public Function Retrieve(src As String) As iTextSharp.text.Image Implements IImageProvider.Retrieve 
     Return Nothing 
    End Function 

    Public Sub Store(src As String, img As iTextSharp.text.Image) Implements IImageProvider.Store 

    End Sub 
End Class 

要使用它,你只需要使用SetImageProvider方法將其綁定到你的HtmlPipelineContext對象:

htmlContext.SetImageProvider(New BlankImageProvider()) 

另外,我不知道如果這個因素存在,但是你的HTML在幾個地方在技術上不準確。例如,您在HTML屬性中使用px,但實際上您只能使用帶百分號的數字或數字。例如,cellpadding="0px"無效,應該是cellpadding="0"。您至少還有一個<table>標籤,該標籤直接是<tr>的孩子,但應該有中介<td><th>標籤。這些事情可能會或可能不會影響iTextSharp的速度,但它可能使事情更具前瞻性。