예제 #1
0
파일: PdfDocument.cs 프로젝트: Kiesum/hets
        public static async Task <JsonResponse> BuildPdf(INodeServices nodeServices, PdfRequest request)
        {
            try
            {
                JObject      options = JObject.Parse(request.Options);
                JsonResponse result  = await nodeServices.InvokeAsync <JsonResponse>(request.PdfJsUrl, request.Html, options);

                return(result);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
예제 #2
0
파일: PdfDocument.cs 프로젝트: rstens/hets
        public static byte[] BuildPdf(IConfigurationRoot configuration, PdfRequest request)
        {
            try
            {
                // validate request
                if (string.IsNullOrEmpty(request.PdfFileName))
                {
                    throw new ArgumentException("Missing PdfFileName");
                }

                if (string.IsNullOrEmpty(request.Html))
                {
                    throw new ArgumentException("Missing Html content");
                }

                // pass the request on to the new (weasy) Pdf Micro Service
                string pdfService = configuration["Constants:WeasyPdfService"];

                if (string.IsNullOrEmpty(pdfService))
                {
                    throw new ArgumentException("Missing PdfService setting (WeasyPdfService)");
                }

                // append new filename
                pdfService = pdfService + "?filename=" + request.PdfFileName;

                // call the microservice
                HttpClient          client        = new HttpClient();
                StringContent       stringContent = new StringContent(request.Html);
                HttpResponseMessage response      = client.PostAsync(pdfService, stringContent).Result;

                // success
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    var bytetask = response.Content.ReadAsByteArrayAsync();
                    bytetask.Wait();

                    return(bytetask.Result);
                }

                throw new ApplicationException("PdfService Error (" + response.StatusCode + ")");
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }