1
I don’t know how to solve this problem:
As noted by Marcos Marques this is about the size for each record in total of 4 thousand records.
Where can I improve the code:
public ActionResult ExportarPDF(ProcessamentoRegistrosDTO pProcessamentoRegistrosDTO)
{
try
{
#region :: variáveis ::
string _nomeArquivo = string.Empty;
TempData["Id"] = null;
TempData["DtInicio"] = null;
TempData["DtFim"] = null;
TempData["Parametro"] = null;
TempData["CodigoSituacao"] = null;
TempData["ServicosId"] = null;
#endregion
#region :: executa o filtro da tela ::
IEnumerable<ProcessamentoRegistros> _processamentoRegistros = null;
if (pProcessamentoRegistrosDTO.DtInicio != null || pProcessamentoRegistrosDTO.DtFim != null)
{
_processamentoRegistros = _IRepositorio.ListarProcessamentoRegistros()
.Where(x => x.DataInclusao.Value.Date >= pProcessamentoRegistrosDTO.DtInicio);
if (pProcessamentoRegistrosDTO.DtInicio != null && pProcessamentoRegistrosDTO.DtFim != null)
{
_processamentoRegistros = _processamentoRegistros.Where(x => x.DataInclusao >= pProcessamentoRegistrosDTO.DtInicio
&& x.DataInclusao.Value.Date <= pProcessamentoRegistrosDTO.DtFim);
}
else if (pProcessamentoRegistrosDTO.DtInicio != null && pProcessamentoRegistrosDTO.DtFim == null)
{
_processamentoRegistros = _processamentoRegistros.Where(x => x.DataInclusao.Value.Date >= pProcessamentoRegistrosDTO.DtInicio);
}
else
{
_processamentoRegistros = _processamentoRegistros.Where(x => x.DataInclusao.Value.Date <= pProcessamentoRegistrosDTO.DtFim);
}
}
else
{
var mesAtual = DateTime.Now.Month;
_processamentoRegistros = _IRepositorio.ListarProcessamentoRegistros()
.Where(x => x.DataInclusao.Value.Month == mesAtual);
}
if (!string.IsNullOrWhiteSpace(pProcessamentoRegistrosDTO.Parametro))
_processamentoRegistros = _processamentoRegistros.Where(x => x.Parametro == pProcessamentoRegistrosDTO.Parametro);
if (pProcessamentoRegistrosDTO.IdServico > 0)
_processamentoRegistros = _processamentoRegistros.Where(x => x.IdServico == pProcessamentoRegistrosDTO.IdServico);
if (pProcessamentoRegistrosDTO.CodigoSituacao > 0)
_processamentoRegistros = _processamentoRegistros.Where(x => x.CodigoSituacao == pProcessamentoRegistrosDTO.CodigoSituacao);
ServicosRepositorio _repoServicos = new ServicosRepositorio();
IEnumerable<Servicos> _servicos = _repoServicos.ListarServicos();
IEnumerable<ProcessamentoRegistrosDTO> _processamentoServico = from prg in _processamentoRegistros
join servicos in _servicos on prg.IdServico equals servicos.Id into outServicos
from servicos in outServicos.DefaultIfEmpty()
select new ProcessamentoRegistrosDTO
{
Id = prg.Id,
Parametro = prg.Parametro,
Descricao = servicos.Descricao,
DocumentoHtml = prg.DocumentoHtml,
DocumentoPDF = prg.DocumentoPDF
};
List<ProcessamentoRegistrosDTO> _lstProcessamento = new List<ProcessamentoRegistrosDTO>();
_lstProcessamento = _processamentoServico.ToList();
#endregion
if (_processamentoServico != null && _processamentoServico.Count() > 0)
{
#region :: converte arquivo html para pdf ::
List<byte[]> meusPDFs = new List<byte[]>();
foreach (var item in _processamentoServico)
{
_nomeArquivo = "Documento_Fiscal_" + DateTime.Now.ToString().Replace(" ", "_").Replace("/", "_").Replace(":", "_") + ".zip";
if (!string.IsNullOrWhiteSpace(item.DocumentoHtml))
{
MemoryStream file = null;
var pechkin = Factory.Create(new GlobalConfig());
var _pdf = pechkin.Convert(new ObjectConfig()
.SetLoadImages(true).SetZoomFactor(1)
.SetPrintBackground(true)
.SetScreenMediaType(true)
.SetCreateExternalLinks(true)
.SetIntelligentShrinking(true).SetCreateInternalLinks(true)
.SetAllowLocalContent(true), item.DocumentoHtml.ToString());
file = new MemoryStream();
file.Write(_pdf, 0, _pdf.Length);
meusPDFs.Add(_pdf);
}
}
#endregion
string _descricao = string.Empty;
#region :: compacta e faz o download do arquivo pdf ::
using (var compressedFileStream = new MemoryStream())
{
//Informações sobre o GetEncoding: https://msdn.microsoft.com/en-us/library/system.text.encodinginfo.getencoding(v=vs.110).aspx
Encoding nomeArquivoEncoding = Encoding.GetEncoding(850);
using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, false, nomeArquivoEncoding))
{
int i = 0;
if (!string.IsNullOrWhiteSpace(_lstProcessamento[i].DocumentoHtml))
{
foreach (var pdf in meusPDFs)
{
_descricao = RemoveSpecialCharacters(RemoveDiacritics(_lstProcessamento[i].Descricao), false);
var zipEntry = zipArchive.CreateEntry(_lstProcessamento[i].Id + "_" + _lstProcessamento[i].Parametro + "_" + _descricao + ".pdf");
using (var originalFileStream = new MemoryStream(pdf))
{
using (var zipEntryStream = zipEntry.Open())
{
originalFileStream.CopyTo(zipEntryStream);
}
}
i++;
}
}
}
return new FileContentResult(compressedFileStream.ToArray(), "application/zip") { FileDownloadName = "CPFL Renováveis – Controle de Documentos Fiscais " + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".zip" };
}
#endregion
}
else
{
return RedirectToAction("Index", "Documento");
}
}
catch (Exception ex)
{
TempData["mensagemErro"] = string.Format("Download não efetuado! " + ex.Message.ToString());
return RedirectToAction("Index", "Documento");
}
}
I may be talking pumpkin, but in
foreach
, try adding aThread.Sleep(5);
at the end, once this solved a problem of mine of Outofmemory.– Francisco
What would be the 5 ? since
Thread.Sleep
is based on milliseconds, example a second would beThread.Sleep(1000)
.– hard123
Exactly, five milliseconds, it’s a little time for the show to take a breath.
– Francisco