示例#1
0
        public void GetPageSettings_PaperSizeSetNull_ExpectDefault()
        {
            var convertOptions = new ConverterOptions();
            var actual         = SettingsConverter.GetPageSettings(convertOptions);

            Assert.Equal(794, actual.Width);
            Assert.Equal(1123, actual.Height);
        }
示例#2
0
        public void GetPageSettings_MarginsSetNull_ExpectDefault()
        {
            var convertOptions = new ConverterOptions();
            var actual         = SettingsConverter.GetPageSettings(convertOptions);

            Assert.Equal(38, actual.Left);
            Assert.Equal(38, actual.Top);
            Assert.Equal(38, actual.Right);
            Assert.Equal(38, actual.Bottom);
        }
示例#3
0
        public void GetPageSettings_PaperSizeSetA5Portrait_ExpectA5Portrait()
        {
            var convertOptions = new ConverterOptions {
                Paper = new Paper {
                    Size = "A5"
                }
            };
            var actual = SettingsConverter.GetPageSettings(convertOptions);

            Assert.Equal(794, actual.Height);
            Assert.Equal(562, actual.Width);
        }
示例#4
0
        public void GetPageSettings_PaperSizeSetA4Landscape_ExpectA4Landscape()
        {
            var convertOptions = new ConverterOptions {
                Paper = new Paper {
                    Size = "A4", Orientation = "Landscape"
                }
            };
            var actual = SettingsConverter.GetPageSettings(convertOptions);

            Assert.Equal(794, actual.Height);
            Assert.Equal(1123, actual.Width);
        }
        public IActionResult Post(ConverterOptions converterOptions)
        {
            System.IO.Stream resultStream = null;
            var startTime = DateTime.Now;

            var tempFileGuid = Guid.NewGuid();

            _logger.LogInformation($"Conversion started: {tempFileGuid} from MD to {converterOptions.To.ToUpper()} / {startTime}");
            var htmlFileName    = tempFileGuid + ".html";
            var cssFileTheme    = System.IO.Path.Combine(_env.WebRootPath, "css/markdown.css");
            var cssContent      = System.IO.File.ReadAllText(cssFileTheme);
            var resultedContent = converterOptions.To.ToLower() != "html"
                ? string.Concat("<style>", cssContent, "</style>\n\n", converterOptions.Content)
                : converterOptions.Content;

            var file = new System.IO.MemoryStream(Encoding.UTF8.GetBytes(resultedContent));

            Aspose.Html.Cloud.Sdk.Api.Model.AsposeResponse response;
            try
            {
                response = _importApi.PostImportMarkdownToHtml(file, htmlFileName);
            }
            catch (AggregateException ae)
            {
                ae.Handle((x) =>
                {
                    _logger.LogError($"Error file uploading {tempFileGuid}. {x.Message}");
                    return(true);
                });
                throw;
            }
            catch (Exception ex)
            {
                _logger.LogError($"Error file uploading {tempFileGuid}. {ex.Message}");
                throw;
            }


            switch (converterOptions.To.ToLower())
            {
            case "pdf":
                _logger.LogInformation($"Converter options: {converterOptions.Paper.Size} {converterOptions.Paper.Width}x{converterOptions.Paper.Height}");
                var options = SettingsConverter.GetPageSettings(converterOptions);
                _logger.LogInformation($"Calculated options: {options}");
                var streamResponse = _conversionApi.GetConvertDocumentToPdf(htmlFileName,
                                                                            options.Width, options.Height,
                                                                            options.Left, options.Bottom,
                                                                            options.Right, options.Top);
                if (streamResponse != null && streamResponse.Status == "OK")
                {
                    try
                    {
                        resultStream = streamResponse.ContentStream;
                    }
                    catch (Exception ex)
                    {
                        _logger.LogError($"Conversion error: {ex.Message}");
                        throw;
                    }
                    _contentType = "application/pdf";
                }
                break;

            case "html":
                try
                {
                    if (response != null && response.Status == "OK")
                    {
                        resultStream = _storageApi.DownloadFile(htmlFileName).ContentStream;
                    }
                }
                catch (Exception ex)
                {
                    _logger.LogError("Error file downloading {0} {1}", tempFileGuid, ex.Message);
                    throw;
                }

                _contentType = "text/html; charset=utf-8";
                break;

            default:
                throw new NotImplementedException($"MD-to-{converterOptions.To} is not supported.");
            }
            if (resultStream == null)
            {
                _logger.LogError("Error MD to PDF conversion {0} {1}", tempFileGuid, htmlFileName);
                throw new Exception("Error MD to PDF conversion");
            }
            var finishTime = DateTime.Now;

            _logger.LogInformation(message: $"Conversion completed: {tempFileGuid} / {finishTime}");
            _logger.LogInformation(message: $"Conversion time: {tempFileGuid} / {finishTime.Subtract(startTime).Milliseconds}ms");
            //TODO: Implement gather stats
            _statisticalService.IncrementCounter(converterOptions.MachineId);
            return(File(resultStream, _contentType));
        }