private void FinalizePdf(string htmlStr, string source, string finalDestination) { ConverterProperties converterProperties = new ConverterProperties(); string intermediateDestination = System.IO.Path.GetTempFileName().Split(".")[0] + ".pdf"; PdfWriter writerIntermedio = new PdfWriter(intermediateDestination); PdfDocument pdf = new PdfDocument(writerIntermedio); pdf.SetDefaultPageSize(PageSize.A4.Rotate()); IEventHandler handler = new PdfBasicSchema(pdf, source, _configuration["ResourcesRoute"]); pdf.AddEventHandler(PdfDocumentEvent.START_PAGE, handler); HtmlConverter.ConvertToPdf(htmlStr, pdf, converterProperties); pdf.Close(); writerIntermedio.Close(); PdfBasicSchema.GenerateFooter(intermediateDestination, finalDestination); }
public async Task <MemoryStream> CreatePdf(ProductFiltersDto filtersDto) { string directory = _configuration["PdfRoute"]; string finalDestination = directory + "/" + DateTime.Now.ToString().Replace("/", "_").Substring(0, 11) + ".pdf"; string source = PdfBasicSchema.GenerateHeader( "LISTADO DE PRODUCTOS", "sdfsdf", //codigo form DateTime.Now, //fecha form "01", //version form _configuration["ResourcesRoute"], "todoHogarLogo.png" ); string htmlStr = "<html>" + "<head>" + "<link rel='stylesheet' type='text/css' href='" + _configuration["ResourcesRoute"] + eRoutes.Html + "/style.css'>" + "</head>" + "<body style='font-family: Arial, Helvetica, sans-serif; margin-top: 4.1cm;'>"; //htmlStr = htmlStr.Insert(htmlStr.Count(), // "<table class='no-border mb-1'>" + // "<tr>" + // "<td class='no-border'></td>" + // "<td class='no-border'></td>" + // "<td class='align-right no-border i' ><span class='mr-05 bold'>Fecha:</span></td>" + // "<td style='box-sizing: border-box; width: 25%'><span class='ml-05'>{valorFecha}</span></td>" + // "</tr>" + // "</table>"); htmlStr = htmlStr.Insert(htmlStr.Count(), "<table class='no-border mb-1'>" + "<tr class='align-center bold'>" + "<td>Nombre</td>" + "<td>Descripción</td>" + "<td>Código</td>" + "<td>Categoría</td>" + "<td>Proveedor</td>" + "<td>Marca</td>" + "<td>Precio de venta</td>" + "</tr>" + "<dinamic></dinamic>" + "</table>"); var products = (await _productRepository.GetAll(x => x.Include(p => p.Category).Include(p => p.Vendor))) .AsQueryable().Where(filtersDto.GetExpresion()).OrderBy(x => x.Name); foreach (var prod in products) { var salePrice = (await _priceRepository.GetAll()).Where(p => p.ProductId == prod.Id && p.PriceType == ePriceTypes.SalePrice).OrderByDescending(x => x.DateTime).FirstOrDefault(); htmlStr = AppendDynamicField(htmlStr, prod.Name, prod.Description, prod.Code, prod.Category.Name, prod.Vendor.Name, prod.Brand, salePrice.Value); } htmlStr.Replace("<dinamic></dinamic>", ""); //string saltoPagina = "<div style='page-break-after: always;'></div>"; htmlStr = htmlStr.Insert(htmlStr.Count(), "</body></html>"); FinalizePdf(htmlStr, source, finalDestination); return(await ReturnResponse(finalDestination)); }