/// <summary>
        ///   The render async.
        /// </summary>
        /// <param name="request">
        ///   The request.
        /// </param>
        /// <returns>
        ///   The <see cref="Task" />.
        /// </returns>
        public async Task <RenderedTemplate> Render(RenderTemplateRequest request)
        {
            const string path = "/templates/render.json";

            RenderedTemplate response = await Post <RenderedTemplate>(path, request);

            return(response);
        }
Example #2
0
        public async Task <IActionResult> RenderTemplate(RenderTemplateRequest data)
        {
            try
            {
                Subscription subscription = _context.Subscriptions.FirstOrDefault(a => a.ApiKey == data.Key);

                byte[] randomKey = new byte[15];
                using (RandomNumberGenerator generator = RandomNumberGenerator.Create())
                {
                    generator.GetBytes(randomKey);
                }

                string randomFileName = Convert.ToBase64String(randomKey);

                Regex rgx = new Regex("[^a-zA-Z0-9 -]");
                randomFileName = rgx.Replace(randomFileName, "");

                string filename = $"{randomFileName}.pdf";

                Render render = new Render()
                {
                    CreateDate     = DateTime.UtcNow,
                    Data           = JsonConvert.SerializeObject(data.Data),
                    RenderType     = RenderType.Url,
                    SubscriptionId = subscription.Id,
                    TemplateId     = data.TemplateId,
                    BlobUrl        = $"https://pdfinarystorage.blob.core.windows.net/pdfinary/{filename}"
                };

                _context.Renders.Add(render);
                _context.SaveChanges();

                Template template = _context.Templates.FirstOrDefault(a => a.Id == data.TemplateId);

                using (WebClient client = new WebClient())
                {
                    string urlOut = $"http://pdfinary.com/Renders/Preview/{render.Id}";

                    MemoryStream pdfMemoryStream = new MemoryStream(client.DownloadData($"https://pdf-render-pdfinary.herokuapp.com/api/render?url={urlOut}&scrollPage={template.ScrollPage}&emulateScreenMedia={template.EmulateScreenMedia}&pdf.scale={template.Scale}&pdf.format={template.PageFormat}&pdf.landscape={template.IsLandscape}&pdf.margin.top={template.MarginTop}mm&pdf.margin.bottom={template.MarginBottom}mm&pdf.margin.left={template.MarginLeft}mm&pdf.margin.right={template.MarginRight}mm"));

                    await _blobStorageService.UploadFileAsync(pdfMemoryStream, filename);

                    pdfMemoryStream.Position = 0;

                    return(new FileStreamResult(pdfMemoryStream, "application/pdf"));
                }
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }