1

I am working with Kendo MVC and I want to render an uploaded PDF into a Tabstrip.

When getting the PDF file to show in the tab, I am calling my controller and subsequent service, getting the file data as a byte array and returning data into its respective div using the following code.

public FileStreamResult GetRxPdf(int prescriptionId)
{
    var retVal = _service.GetRxPdf(prescriptionId);
    byte[] byteArray = retVal.FileData;
    MemoryStream pdfStream = new MemoryStream();
    pdfStream.Write(byteArray, 0, byteArray.Length);
    pdfStream.Position = 0;
    return new FileStreamResult(pdfStream, retVal.ContentType);
}

The rendered output is not the PDF, but is as follows

%PDF-1.7 %���� 1 0 obj <>/OutputIntents[<>] /Metadata 148 0 R/ViewerPreferences 149 0 R>> endobj 2 0 obj <> endobj 3 0 obj <>/XObject<>/ProcSet[/PDF/Text/ImageB/ImageC/ImageI] >>/Annots[ 7 0 R 10 0 R 11 0 R 21 0 R] /MediaBox[ 0 0 595.32 841.92] /Contents 4 0 R/StructParents 0>> endobj 4 0 obj <> stream x��]Ys�F~W��ɔ5�\�I�֒��[9[�yH�@S�̲�����7���=Ip��P�TE��3������1�>[,��&�e��7�ϖ��������������?�N/&���r~w����%~��lr5[|�mq�������$%�W׊e!I8+jAIÊ����ׯ��㣳��ӗ������|w|��eA�H�D����uqy3�����F������G/`�/�G��m�����4r�g����m�*��/s^%�HQ�R�]䛋����]��h1S>�W������R^�0>���՘�Q1(�'l�zL���z>棇��pm0����=�±7�;�� ?��]��ů�>�� ��U�~�O?��a�^���>�A+�xD %�Ԍ�H� /�������$MD�~�A2V6�Ѣ��Q....more file data here... 0000166807 00000 n trailer <] >> startxref 166853 %%EOF

When the data comes back via an ajax call it this is how it is passed to the div

 $("#divRxPdf").html(data);

I would be grateful if anyone could help me with this and show the PDF in the Div accordingly.

4
  • Maybe you need to use something like pdf to html. Commented Oct 22, 2018 at 10:36
  • whats your column data type to store pdf content data? Commented Oct 22, 2018 at 10:49
  • data type in the database is VarBinary(max), content type is stored as a string as 'application/pdf' Commented Oct 22, 2018 at 10:58
  • @MihaiAlexandru-Ionut the PDF is a scanned document, so making this HTML is not an option Commented Oct 22, 2018 at 10:59

2 Answers 2

2

Update:

PDF needs to be embeded in the HTML with <embed> or <iframe> . You could also use library such as pdf.js to render pdf files with some controls.

The pdf.js readme has samples and how to: https://github.com/mozilla/pdf.js


You can return the pdf as file by passing the binary using retun File() method

public ActionResult GetRxPdf(int prescriptionId)
 {
    var retVal = _service.GetRxPdf(prescriptionId);
    byte[] byteArray = retVal.FileData;      
   return File(byteArray , "application/pdf" , "Filename.pdf");
}
Sign up to request clarification or add additional context in comments.

7 Comments

That was one of the first things I tried and I got the same result, using ActionResult, FileResult, FileContentResult and FileStreamResult
ideally i dont want to use an iframe, because i want an element of control over the PDF where possible
Have you tried pdf.js ?
the embed is the way to go in this instance... thanks for your help
@SimonPrice If your still looking, I was just working on this. Here's is an ASP MVC example I added.
|
0

You can use object tag to achieve it.

$obj = $('<object>'); 
$obj.attr("data","Url to PDF");
$obj.attr("type","application/pdf");

$("#pdfdiv_content").append($obj);

For more details refer this link

2 Comments

im not converting html to pdf, the pdf is already uploaded and stored in the database, i have the data, and am passing back its content and its type
plus, that didnt work, it doesnt show me the file data anymore, just shows me nothing, inepcting the code, the file data is in the div, but nothing rendered

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.