I am trying to send a Byte Array
of Conversion - HTML to PDF using SelectPDF
in C#. The Sample code I given below. I'm having a WebAPI, in that I'm doing this. I need to send the PDF document as an byte array and need to display in the client UI. The following code is not performing.
Refer documentation of SelectPDF https://selectpdf.com/docs/M_SelectPdf_PdfDocument_Save_3.htm
string html = "<html><body>hello world</body></html>";
PdfDocument doc = converter.ConvertHtmlString(html, "");
doc.Save(HttpContext.Current.Response, true, "C:\MyProject\Pdf\Sample.pdf");
If I make the API return type as HttpResponseMessage
, then its working, instead of returning the HttpContext.Current.Response (Return type is HttpResponse), its not working.
Working code
Saving the pdf to local using the following code doc.Save(URL)
and then need to read the file (C# 4.0: Convert pdf to byte[] and vice versa)
Kindly assist me how to return the Response using doc.Save(HttpContext.Current.Response, true, "C:\MyProject\Pdf\Sample.pdf");
Note: Kindly provide the solution without saving the file in local (i.e., Server Local)
Working C# Code:
public HttpResponseMessage GetSamplePDF() {
string html = "<html><body>hello world</body></html>";
PdfDocument doc = converter.ConvertHtmlString(html, "");
doc.Save("C:\MyProject\Pdf\Sample.pdf");
pdfData = System.IO.File.ReadAllBytes("C:\MyProject\Pdf\Sample.pdf");
var result = new HttpResponseMessage(HttpStatusCode.OK) {
Content = new ByteArrayContent(reportdata)
};
result.Content.Headers.ContentDisposition =
new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") {
FileName = "SamplePDF"
};
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
return result;
}
Moreover the UI code is Embed a Blob using PDFObject