Beispiel #1
0
        public ActionResult TestHtml()
        {
            string fileName    = Guid.NewGuid().ToString();
            string pdfFileName = fileName + ".pdf";

            string pdfFileNamePage2 = fileName + "_Page2.pdf";

            using (var file = System.IO.File.OpenRead(Path.Combine(_hostingEnvironment.WebRootPath, "second_page.html")))
            {
                _htmlApi.PostConvertDocumentToPdf(file, pdfFileNamePage2);
            }
            string bearerToken = string.Empty;
            string token       = _httpContextAccessor.HttpContext.Request.Headers["Authorization"];
            Regex  regexp      = new Regex(@"Bearer\s+(?<token>\S+)", RegexOptions.IgnoreCase);
            Match  match       = regexp.Match(token);

            if (match.Success)
            {
                bearerToken = match.Groups["token"].Value;
            }
            StorageApi sa = new StorageApi(new Aspose.Html.Cloud.Sdk.Client.Authentication.JwtToken
            {
                Token            = bearerToken,
                IssuedOn         = DateTime.Today,
                ExpiresInSeconds = 86400
            }, basePath);

            Aspose.Html.Cloud.Sdk.Api.Model.StreamResponse sr = sa.DownloadFile(pdfFileNamePage2);
            return(new FileStreamResult(sr.ContentStream, "application/pdf"));
        }
        public void Run()
        {
            string name = "testpage4_embcss.html";
            string path = Path.Combine(CommonSettings.LocalDataFolder, name);

            if (!File.Exists(path))
            {
                throw new FileNotFoundException("File not found in the Data folder", name);
            }

            string folder  = "/Html/Testout/Conversion";
            string storage = null;

            int width        = 800;
            int height       = 1200;
            int leftMargin   = 15;
            int rightMargin  = 15;
            int topMargin    = 15;
            int bottomMargin = 15;
            int resolution   = 96;

            string ext     = (Format == "tiff") ? "tif" : ((Format == "jpeg") ? "jpg" : Format);
            string outFile = $"{Path.GetFileNameWithoutExtension(name)}_converted_at_{DateTime.Now.ToString("yyyyMMdd_hhmmss")}.{ext}";
            string outPath = Path.Combine(folder, outFile).Replace('\\', '/');

            using (Stream srcStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                IConversionApi convApi    = new HtmlApi(CommonSettings.ClientId, CommonSettings.ClientSecret, CommonSettings.BasePath);
                IStorageApi    storageApi = new StorageApi((ApiBase)convApi);

                if (!storageApi.FileOrFolderExists(folder, storage))
                {
                    ((IStorageFolderApi)storageApi).CreateFolder(folder, storage);
                }
                AsposeResponse response = null;
                string         dataType = Path.GetExtension(name).Replace(".", "");
                // call SDK methods that convert HTML document to supported out format
                switch (Format)
                {
                case "pdf":
                    outFile += ".pdf";
                    response = convApi.PostConvertDocumentToPdf(
                        srcStream, outPath, width, height, leftMargin, rightMargin, topMargin, bottomMargin, storage);
                    break;

                case "xps":
                    response = convApi.PostConvertDocumentToXps(
                        srcStream, outPath, width, height, leftMargin, rightMargin, topMargin, bottomMargin, storage);
                    break;

                case "jpeg":
                case "bmp":
                case "png":
                case "tiff":
                case "gif":
                    response = convApi.PostConvertDocumentToImage(
                        srcStream, Format, outPath, width, height,
                        leftMargin, rightMargin, topMargin, bottomMargin,
                        resolution, storage);
                    break;

                default:
                    throw new ArgumentException($"Unsupported output format: {Format}");
                }

                if (response != null && response.Status == "OK")
                {
                    storageApi = new StorageApi((ApiBase)convApi);
                    if (storageApi.FileOrFolderExists(outPath))
                    {
                        Console.WriteLine(string.Format("\nResult file uploaded to: {0}", outPath));
                    }
                }
            }
        }