private async Task <byte[]> GetPDF(byte[] file, string id) { byte[] ret = null; string baseUrl = _configuration["baseUrl"]; string inputFilename = @"wwwroot/pdf-template/cert.html"; string imgsrc = $"data:image/png;base64,{QR.GetPureBase64($"{baseUrl}/Contract/Certificate?id={id}")}"; HtmlDto dto = new HtmlDto() { html = System.Web.HttpUtility.HtmlEncode( Encoding.UTF8.GetString( System.IO.File.ReadAllBytes(inputFilename))) .Replace("{certificateid}", id) .Replace("{qrlink}", imgsrc), footerPath = $"{baseUrl}/pdf-template/footer.html" }; string jsonString = Newtonsoft.Json.JsonConvert.SerializeObject(dto); string uri = _configuration["pdfApi"]; var content = new StringContent(jsonString, Encoding.UTF8, "application/json"); var response = await client.PostAsync($"{uri}", content).ConfigureAwait(false); var file2 = await response.Content.ReadAsByteArrayAsync(); ret = Concat.DoConcat(file, file2); return(ret); }
public HttpResponseMessage GeneratePDF([FromBody] HtmlDto dto) { //string inputFilename = "cert-simple2.html"; //string outputFilename = "cert-simple2.pdf"; //string html = Encoding.UTF8.GetString(File.ReadAllBytes(inputFilename)); //byte[] pdf = new SimplePechkin(new GlobalConfig()).Convert(html); //string htmlDecoded = HttpUtility.HtmlDecode(dto.html); var pdfBuilder = Pdf.From(HttpUtility.HtmlDecode(dto.html)); // pdfBuilder = pdfBuilder.WithObjectSetting("header.htmlUrl", dto.footerPath); if (!String.IsNullOrEmpty(dto.footerPath)) { pdfBuilder = pdfBuilder.WithGlobalSetting("margin.top", "2cm") .WithGlobalSetting("margin.bottom", "2cm") //.WithObjectSetting("footer.center","THE FOOTER") .WithObjectSetting("footer.htmlUrl", dto.footerPath); } var pdf = pdfBuilder.Content(); //HttpContent content = new StreamContent(); ByteArrayContent content = new ByteArrayContent(pdf); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf"); content.Headers.Add("content-disposition", "attachment;filename=myFile.pdf"); return(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = content }); }