在尝试将HTML文件转换为PDF文件时,我的程序似乎挂起了pdf.save(ms)

时间:2017-08-23 13:21:16

标签: html pdf filestream memorystream pdfsharp

我有两个程序,一个根据SQL DB中的数据写一个html文件(这很好用)保存文件后,下面的程序应该把它拿起并转换成PDF,第一个程序要邮寄它给客户。问题在于转换,我一直在苦苦挣扎数周,尝试过iTextSharp,wkhtmltopdf,Migradoc和Pdfsharp等。

任何帮助都将不胜感激。

以下是转换器的代码:

static void Main(string[] args)
{
    Program prg = new Program();

    Console.Write("Please enter File Name"+"\n");
    string Filename = Console.ReadLine();
    string FilePath = @"C:\Users\BB\Documents\Reports\" + Filename;
    var fs = new FileStream(FilePath, FileMode.Open, FileAccess.Read,
                              FileShare.ReadWrite | FileShare.Delete);
    string content;
    using (StreamReader reader = new StreamReader(fs))
    {
        content = reader.ReadToEnd();
    }

    PdfSharpConvert(content, FilePath);
}

public static void PdfSharpConvert(string html,string Filepath)
{

    Byte[] res = null;
    using (MemoryStream ms = new MemoryStream())
    {

        var pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.A4);
        pdf.Save(ms); ---> this is where the code hangs
        res = ms.ToArray();
        FileStream file = new FileStream("C:\\Users\\BB\\Documents\\Reports\\Report_1499.pdf", FileMode.Create, FileAccess.Write);
        File.WriteAllBytes(Filepath,res);

    }
}

这是HTML文件的内容:

      <html xmlns="http://www.w3.org/1999/xhtml"><head><title>Untitled Page</title><style type="text/css">
      PRE   {page-break-before: always;}

      @media print
      {
      .footer
      {
      position: absolute; bottom: 0px;
      }
      }

      </style></head><body style="margin:0"><div style="width: 100%; position:absolute; top: 0; z-index:-1"><img src="logo1.png" height="257" border="0" alt="" /></div><div style="margin-bottom: 100px;"><br /></div><table style="font-family: Verdana, Geneva, sans-serif; font-size:13px;" width="750" cellspacing="0" cellpadding="5" border="0"><tr><td width="400" align="left"><font color="#3b6f85"><b>HELPMEKAAR-STUDIEFONDS</b></font><br /><font color="#000000">
              Address: PO Box 1223<br />Parrow   7499<br /><br /><br /></font><font color="#000000">
              Postal Code: <br /></font></td></tr></table><table style="font-family: Verdana, Geneva, sans-serif; font-size:13px; margin-top:15px" width="780" border="0" cellspacing="0" cellpadding="5"><tr><td align="left" style="font-family: Verdana, Geneva, sans-serif; font-size:13px; border-bottom:1px medium; border-top:0px; border-left:0px; border-right:0px" width="780">
            Acccount Number: </td></tr><tr><td align="left" style="font-family: Verdana, Geneva, sans-serif; font-size:13px; border-bottom:1px medium; border-top:0px; border-left:0px; border-right:0px" width="780">
            Date: </td></tr><tr><td align="left" style="font-family: Verdana, Geneva, sans-serif; font-size:13px; border-bottom:1px medium; border-top:0px; border-left:0px; border-right:0px" width="780">
            Statement Period:  - <br /></td></tr></table><br /><table style="font-family: Verdana, Geneva, sans-serif; font-size:13px;" width="780" cellspacing="0" cellpadding="5"><tr><td width="200" valign="top"><b>Agreement Details</b></td></tr><tr><td width="200" valign="top" bgcolor="#ffffff">
            Asset Description:<br />
            In the name of:<br />
            Financed Amount:<br />
            Finance Contract Start Date:<br />
            Finance Contract Expiry Date:<br />
            Contract Term:<br />
            Interest Rate:<br />
            Balance Outstanding:<br />
            Installment Amount:<br />
            Installment Frequency:<br />
            Outstanding Capital Balance:<br />
            Amount in Advance:<br />
            Amount in Arrears:<br /></td><td width="380" valign="top" bgcolor="#ffffff"><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /><br /></td></tr></table><br /><br /><table style="font-family: Verdana, Geneva, sans-serif; font-size:13px;" width="780" cellspacing="0" cellpadding="5"><tr><td width="400" valign="top"><b>Summary of Transactions for Statement Period</b></td></tr></table><table style="font-family: Verdana, Geneva, sans-serif; font-size:13px;" width="780" cellspacing="0" cellpadding="5"><tr><td width="200" valign="top" bgcolor="#ffffff">
            Payments Received:<br />
            Interest:<br />
            Fees Levied:<br />
            Insurance Cost Levied:<br />
            Collection Cost:<br />
            Admin Cost:<br />
            Legal Fees Incurred:<br />
            Other Debits / Credits:<br /></td><td width="380" valign="top" bgcolor="#ffffff"><br /><br /><br /><br /><br /><br /><br /><br /></td></tr></table><br /><table style="font-family: Verdana, Geneva, sans-serif; font-size:13px;" width="780" cellspacing="0" cellpadding="5"><tr><td width="200" valign="top"><b>Installment Details</b></td></tr><tr><td width="200" valign="top" bgcolor="#ffffff">
            Installment Due Date:<br />
            Installment Due Amount:<br /><b>Minus</b> Advance:<br /><b>Plus Arrears:</b><br /><b>Amount Due:</b><br /></td><td width="380" valign="top" bgcolor="#ffffff"><br /><br /><br /><br /><br /></td></tr></table><br /><div class="footer"><table style="font-family: Verdana, Geneva, sans-serif; font-size:10px;" width="780" cellspacing="0" cellpadding="5" border="0"><tr><td style="text-align:center"><b>Helpmekaar-Studiefonds</b><br />
            Barry van der Merwe bestuur, met die hulp van drie administratiewe beamptes, mevv. Nita van Zyl, Retha Geldenhuys en Wilma Steenkamp.
            Helpmekaar-Studiefonds McIntyrestraat 72 Parow Posbus 1223 Parow 7499.
            Tel.: 021 930 8155 / 021 930 3195 Faks: 021 930 7436, www.helpmekaarfonds.org
            An authorised Financial Services and Credit Provider.
          </td></tr></table></div><br /><PRE /><table style="font-family: Verdana, Geneva, sans-serif; font-size:13px; margin-top:15px" width="780" border="0" cellspacing="0" cellpadding="5"><tr><td align="left" style="font-family: Verdana, Geneva, sans-serif; font-size:13px; border-bottom:1px medium; border-top:0px; border-left:0px; border-right:0px" width="850">
            Acccount Number: </td></tr><tr><td align="left" style="font-family: Verdana, Geneva, sans-serif; font-size:13px; border-bottom:1px medium; border-top:0px; border-left:0px; border-right:0px" width="850">
            Date: </td></tr><tr><td align="left" style="font-family: Verdana, Geneva, sans-serif; font-size:13px; border-bottom:1px medium; border-top:0px; border-left:0px; border-right:0px" width="850">
            Statement Period:  - <br /></td></tr></table><br /><br /><table style="border:1px medium; font-family: Verdana, Geneva, sans-serif; font-size:13px;" rules="all" frame="border" width="780" cellpadding="5" cellspacing="0" bgcolor="#ffffff" bordercolor="#000000"><tr><th style="border-bottom:1px solid; border-top:1px solid; border-left:1px solid; border-right:1px medium; border-color:#000;" align="left" width="130">Date</th><th style="border-bottom:1px solid; border-top:1px solid; border-left:1px solid; border-right:1px medium; border-color:#000;" align="left" width="450">Transaction</th><th style="border-bottom:1px solid; border-top:1px solid; border-left:1px solid; border-right:1px medium; border-color:#000;" align="left" width="120">Debit</th><th style="border-bottom:1px solid; border-top:1px solid; border-left:1px solid; border-right:1px medium; border-color:#000;" align="left" width="120">Credit</th><th style="border-bottom:1px solid; border-top:1px solid; border-left:1px solid; border-right:1px solid; border-color:#000;" align="left" width="120">Balance</th></tr><tr><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="95"><b>2016-12-01</b></td><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="490"><b>Opening Balance</b></td><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="90" align="right" /><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="90" align="right" /><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px solid; border-color:#000;" width="90" align="right"><b /></td></tr><tr><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="95">2016-12-31</td><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="490">Total Debits</td><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="90" align="right" /><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="90" align="right" /><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px solid; border-color:#000;" width="90" align="right" /></tr><tr><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="95">2016-12-31</td><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="490">Total Credits</td><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="90" align="right" /><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="90" align="right" /><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px solid; border-color:#000;" width="90" align="right" /></tr><tr><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="95"><b>2016-12-31</b></td><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="490"><b>Balance carried forward</b></td><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="90" align="right" /><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px medium; border-color:#000;" width="90" align="right" /><td style="border-bottom:1px solid; border-top:1px medium; border-left:1px solid; border-right:1px solid; border-color:#000;" width="90" align="right"><b /></td></tr></table><br /><br /><table style="font-family: Verdana, Geneva, sans-serif; font-size:10px;" width="780" cellspacing="0" cellpadding="5" border="0"><tr><td style="text-align:center"><b>Helpmekaar-Studiefonds</b><br />
            Barry van der Merwe bestuur, met die hulp van drie administratiewe beamptes, mevv. Nita van Zyl, Retha Geldenhuys en Wilma Steenkamp.
            Helpmekaar-Studiefonds McIntyrestraat 72 Parow Posbus 1223 Parow 7499.
            Tel.: 021 930 8155 / 021 930 3195 Faks: 021 930 7436, www.helpmekaarfonds.org
            An authorised Financial Services and Credit Provider.
          </td></tr></table><br /></body></html>

0 个答案:

没有答案