예제 #1
0
        public ActionResult Get(string pdfCode, [FromQuery(Name = "ids")] List <string> formMapCollectionsIds)
        {
            PdfFormMap pdfFormMap;

            try
            {
                pdfFormMap = _pdfFormRepository.Get(pdfCode);
            }
            catch (EntityNotFoundException)
            {
                return(NotFound($"pdfCode={pdfCode}"));
            }

            if (pdfFormMap == null)
            {
                return(NotFound($"pdfCode={pdfCode}"));
            }
            if (pdfFormMap.Collections?.Count != formMapCollectionsIds?.Count)
            {
                return(BadRequest("ids not of expected size"));
            }

            var collectionsValues = new Dictionary <string, dynamic>();

            if (pdfFormMap.Collections != null)
            {
                for (int i = 0; i < pdfFormMap.Collections.Count; i++)
                {
                    _dynamicRepository.SetContext(pdfFormMap.Collections[i]);
                    try
                    {
                        var value = _dynamicRepository.Get(formMapCollectionsIds[i]);
                        collectionsValues.Add(pdfFormMap.Collections[i], value);
                    }
                    catch (EntityNotFoundException)
                    {
                        return(NotFound($"ids[{i}]={formMapCollectionsIds[i]}"));
                    }
                }
            }

            var templateFilePath = $"{pdfFormMap.PdfCode}.pdf";

            if (!string.IsNullOrEmpty(_pdfFilesOptions.TemplatePath))
            {
                templateFilePath = $"{_pdfFilesOptions.TemplatePath}{Path.DirectorySeparatorChar}{templateFilePath}";
            }

            var generatedFileName = $"{pdfFormMap.PdfCode}-filled-{DateTimeOffset.Now.ToString("yyyyMMddHHmmss")}.pdf";
            var generatedFilePath = generatedFileName;

            if (!string.IsNullOrEmpty(_pdfFilesOptions.GeneratedPath))
            {
                generatedFilePath = $"{_pdfFilesOptions.GeneratedPath}{Path.DirectorySeparatorChar}{generatedFilePath}";
            }

            FillPdfForm(templateFilePath, generatedFilePath, pdfFormMap, collectionsValues);

            //var stream = new FileStream(@"path\to\file", FileMode.Open);
            //return File(stream, "application/pdf", "FileDownloadName.ext");
            var stream = new FileStream(generatedFilePath, FileMode.Open);

            return(File(stream, "application/pdf", generatedFileName));
        }