尝试将字节数组转换为PDF(Rotativa)时尝试转换字节数组时,对象引用未设置为对象的实例

时间:2019-07-16 16:45:48

标签: c# asp.net .net async-await rotativa

尝试将ViewAsPdf转换为字节数组时出现异常

我正在使用.Net Core 2.1的“ Rotativa”库,以将从Web服务获取的html字符串转换为字节数组。最后,通过邮件与另一家书店MailKit发送。 我认为与(WebApi的)“ Imprimir”方法存在异步冲突,该方法返回html字符串和Rotativa库的BuildFile方法,尽管我不清楚。 我之所以这样说,是因为当我提到对Web服务的调用,并加上一条硬编码链时,一切都按预期进行。相反,如果不是,.NET将引发异常“ System.NullReferenceException:'对象引用未设置为对象的实例。'”(我不知道如何放置整个错误堆栈)。 非常感谢您的帮助。

public async void Enviar(string codigo, string numero, string receptorAlias, string receptorCorreo)
    {
        try
        {
            int empresaID = (int)HttpContext.Session.GetInt32("EmpresaID");

            var model = await new DocumentoVentaB().Imprimir(codigo, numero, empresaID);

            //var model = "<html>\r\n\t<head>\r\n\t<style>\r\n\t*{font-family: Arial, Helvetica, Verdana, sans-serif;font-size: 13px;}\r\n\ttd {font-size: 11px;table-layout: fixed;padding:2px 0 2px 0;}\r\n\th1, h2 {text-indent: 10px;}\r\n\t#lado1pdf{width:12%;}\r\n\t#lado2pdf{width:28%;}\r\n\t#lado3pdf{width:20%;vertical-align:top;}\r\n\ttable#buscar{width:100%;border:1px solid #000000;}\r\n\ttable#buscar td {padding:2px 5px 0 5px;font-size: 10px;}\r\n\ttd.titulo   {border-bottom:1px solid #000000;font-weight:bold;color:#000000;font-size:13px;}\r\n\t#capatable{border: 1px solid #000000;width:100%;padding:0;\tmargin:0;}\r\n\t#capatable3{border: 1px solid #000000;width:100%;margin:5px 0 0 0;padding:5px 0 5px 0;}\r\n\t#capatable2{border-left: 1px solid #000000;border-right: 1px solid #000000;border-top: 1px solid #000000;width:200px;font-weight:bold;padding:5px 0 5px 0;margin:10px 0 0 0;font-size:13px;height:15px;}\r\n\t</style>\r\n\t</head><body>\r\n\t<h1>Cotización:001-00005083</h1><hr>\r\n\t</hr><table style='width:100%' cellpadding='2' cellspacing='0'>\r\n\t<tr>\r\n\t<td id='lado1pdf'>FECHA</td><td id='lado2pdf'>: 20/02/2019</td>\r\n\t<td id='lado1pdf'></td><td id='lado2pdf'></td>\r\n\t<td rowspan='6' id='lado3pdf'><span style='font-weight: bold;text-decoration:underline;font-size:14px'>SWITHTRADING                                                </span><br></br><span style='font-weight: bold;font-size:11px'>RUC: 20528474609</spa\r\n\r\n\tn><br></br><span style=';font-size:11px'>Calle Napo 346 (con huallaga)                               <br></br>Telf: 242656 - 232574                         </span></td>\r\n\t</tr><tr>\r\n\t<td>SE&Ntilde;OR</td><td colspan='3'>: ALMACENES MUSAJA E.I.R.L.                                    </td>\r\n\t</tr><tr>\r\n\t<td>DIRECCI&Oacute;N</td><td colspan='3'>: TUPAC AMARU MZA. B LOTE. 7 A.H. HISTOR. CON CON (ALT. KM    </td>\r\n\t</tr><tr>\r\n\t<td>TEL&Eacute;FONO</td><td>:                          </td><td></td><td></td>\r\n\t</tr><tr><td>VENDEDOR</td><td>: RAUL MARTIN GARRIDO AGUERO    </td><td>REFERENCIA</td><td>:   /            </td>\r\n\t</tr></table><table id='buscar' cellpadding='2' cellspacing='0'>\r\n\t<tr>\r\n\t<td class='titulo' style='width:17px'>ITM</td>\r\n\t<td class='titulo' style='width:60px'>CODIGO</td>\r\n\t<td class='titulo' style='width:60px'>MARCA</td>\r\n\t<td class='titulo' style='width:217px'>DESCRIPCION</td>\r\n\t<td class='titulo' style='text-align:center;width:50px'>CANT</td>\r\n\t<td class='titulo' style='width:30px'>U.M.</td>\r\n\t<td class='titulo' style='text-align:center;width:60px'>P.UNIT.</td>\r\n\t<td class='titulo' style='text-align:center;width:60px'>TOTAL</td>\r\n\t</tr><tr>\r\n\t\t<td style='width:17px'>1</td>\r\n\t\t<td style='width:60px'>91001           </td>\r\n\t\t<td style='width:60px'>ESC   </td>\r\n\t\t<td style='width:217px'>MAMMUT JACK AIR/HYDRAULIC (30/60 TN) ESCO                                       </td>\r\n\t\t<td align='right' style='width:50px;'>1</td>\r\n\t\t<td align='right' style='width:30px'>UND</td>\r\n\t\t<td align='right' style='width:60px;'>9416.102</td>\r\n\t\t<td align='right' style='width:60px;'>11111.0000</td>\r\n\t\t</tr></table>\r\n\t<table id='capatable2'>\r\n\t<tr>\r\n\t<td>EN: DOLARES</td>\r\n\t</tr>\r\n\t</table> \r\n\t<table id='capatable'>\r\n\t<tr>\r\n\t<td style='width:33%;font-weight:bold;text-align:center;font-size:13px'>VALOR VENTA: US$ 9416.10</td>\r\n\t<td style='width:33%;font-weight:bold;text-align:center;font-size:13px'>IGV: US$ 1694.90</td>\r\n\t<td style='width:33%;font-weight:bold;text-align:center;font-size:13px'>TOTAL: US$ 11111.00</td>\r\n\t</tr>\r\n\t</table>\r\n\t<table id='capatable3'>\r\n\t<tr>\r\n\t<td style='width:25%;'>CONDICION DE PAGO</td>\r\n\t<td style='width:75%;'>: CONTADO             </td>\r\n\t</tr><tr>\r\n\t<td>VALIDEZ</td>\r\n\t<td>: 10 DIAS</td>\r\n\t</tr><tr>\r\n\t<td>OBSERVACION</td>\r\n\t<td>:                                                                                                     </td>\r\n\t</tr><tr>\r\n\t<td>PLAZO DE ENTREGA</td>\r\n\t<td>: SEGUN STOCK INMEDIATO</td>\r\n\t</tr><tr>\r\n\t<td>LUGAR DE ENTREGA</td>\r\n\t<td>: SEGUN PEDIDO</td>\r\n\t</tr>\r\n\t</table><table style='width:100%'>\r\n\t<tr>\r\n\t<td style='padding:30px 0 0 0;font-size:11px;text-align:center;width:50%'>..........................................................<br></br>Cr&eacute;ditos y cobranzas</td>\r\n\t<td style='padding:30px 0 0 0;font-size:11px;text-align:center;width:50%'>..........................................................<br></br>Vendedor</td>\r\n\t</tr></table></body></html>";

            ViewAsPdf documentoPdf = new ViewAsPdf("~/Views/Gestion/Ventas/DocumentoVenta/ImprimirDocumentoVenta.cshtml", model)
            {
                //FileName = "PDF.pdf",
                PageOrientation = Orientation.Landscape,
                PageSize = Size.A4
            };

            var applicationPDFData = await documentoPdf.BuildFile(ControllerContext);

            util.EnviarCorreo("Ventas",
                              CrossCutting.AplicacionConstantes.CORREO_ELECTRONICO_VENTAS,
                              CrossCutting.AplicacionConstantes.CORREO_ELECTRONICO_CLAVE_VENTAS,
                              CrossCutting.AplicacionConstantes.HOST_CORREO_ELECTRONICO_VENTAS,
                              receptorAlias, receptorCorreo, "Documento de venta",
                              "Se adjunta el documento de venta", applicationPDFData, "documento.pdf",
                              "application/pdf");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

0 个答案:

没有答案