2

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

svick
  • 236,525
  • 50
  • 385
  • 514
  • What's the signature of `PDFDocument.Save()`? Does it accept a `Stream`? If so, it's likely that the `Stream` was meant for input, not output. But I'm not familiar with that one, I've used iText. – Glenn Ferrie Jul 18 '17 at 12:45
  • 2
    @GlennFerrie - Kindly refer the link https://selectpdf.com/docs/M_SelectPdf_PdfDocument_Save_3.htm –  Jul 18 '17 at 12:49
  • `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.` Please show us the exact code that **is** working, and the exact code that **isn't** working. – mjwills Jul 18 '17 at 12:51
  • Got it. Your problem is that the last argument should be a filename, i.e. Sample.pdf. It's the filename when you download. — You shouldn't pass a full path. Forget my earlier comment regarding `Stream` – Glenn Ferrie Jul 18 '17 at 12:53
  • @GlennFerrie - I updated the question. Kindly refer the question it contains the working code. –  Jul 18 '17 at 12:59
  • Is there a way to do this without storing the pdf? – ccoutinho Jul 25 '17 at 10:49

1 Answers1

1

For anyone coming to this via a search engine. All you need to do is not pass an overload and it returns a byte[].

doc.Save();
Syscall
  • 19,327
  • 10
  • 37
  • 52
Kevin Kohler
  • 364
  • 4
  • 21