skip to Main Content

I have a function in JavaScript where I receive bytes. I want to know if I can convert the result parameter to PDF. I am currently converting an HTML string to an HTML file. I’m wondering if I can do the same, but instead convert this to a PDF in an iframe by manipulating the SRC data.

function GetValues(result) {
  $("#DisplayHTML").html(
    $('<iframe>', {
      src: 'data:text/html;charset=utf-8,' + encodeURIComponent(result),
      width: '600px',
      height: '800px'
    })
  );
}

View where I display the element

<div id="DisplayHTML"></div>

2

Answers


  1. Ok, not to beat a dead horse.

    Assuming this byte stream is a 100% valid PDF byte stream?

    Well, as you are noting, if the buffer size is rather large, then your blowing out the browser capacity here.

    So, there are 2 approaches.

    Save the byte stream to disk as a pdf file, and THEN using transmit file. This will not use huge memory, and "stream" in chunks to the browser side as a legitimate file download process. The file size thus should not matter.

    this code:

            string sFileWithPath = Server.MapPath(@"~/Content/CONTROL.pdf");
    
            if (File.Exists(sFileWithPath))
            {
                string sFileNameOnly = Path.GetFileName(sFileWithPath);
                string sMineType = MimeMapping.GetMimeMapping(sFileWithPath);
                FileInfo sFinfo = new FileInfo(sFileWithPath); // get lengh of file
    
                Response.Clear();
                Response.ContentType = sMineType;
                Response.AppendHeader("Content-Length", sFinfo.Length.ToString());
    
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + sFileNameOnly);
                Response.TransmitFile(sFileWithPath);
                Response.End();
    
    
            }
    

    So, in your case, save the byte stream you have as a file, and then use above transmit file – it will "chunk up" the data into smaller byte sized bits and parts.

    So, if you have a byte array, then FIRST save it to the local file system – you have to make up or have a file name.

    eg this:

            // byte[] MyFileData = whatever byte stream
    
            File.WriteAllBytes(sFileWithPath, MyFileData);
    
    
    
            if (File.Exists(sFileWithPath))
            {
                string sFileNameOnly = Path.GetFileName(sFileWithPath);
                string sMineType = MimeMapping.GetMimeMapping(sFileWithPath);
                FileInfo sFinfo = new FileInfo(sFileWithPath); // get lengh of file
    
                Response.Clear();
                Response.ContentType = sMineType;
                Response.AppendHeader("Content-Length", sFinfo.Length.ToString());
    
                Response.AppendHeader("Content-Disposition", "attachment; filename=" + sFileNameOnly);
                Response.TransmitFile(sFileWithPath);
                Response.End();
    
    
            }
    

    It probably possible to do a loop, and using response.binarywrite the byte array in chunks, but the above save as a file and using transmit file probably is a better choice.

    And once again, in closing? That byte stream you have is ALREADY assumed to be in a valid PDF file format. If not, then you need some type of PDF library to convert whatever source format the byte steam is into a valid PDF format before any of the above will work.

    So above can fix and address the "large" file download issue.

    However, a byte stream?

    Well, as noted, the byte steam is going to be a word, PDF, Excel or even a png file.

    Dealing with a byte stream has quite much ZERO do to with converting some source format to another format.

    You will need to adopt some code to convert that byte stream which you note is in HTML format, and you need to take that resulting markup, and convert it into a valid PDF.

    So, at the end of the day here?

    Leave out the byte stream issue – you need code to convert HTML to a pdf format, and NONE of the issues in regards to some byte array matters here one bit.

    Your goal is to FIRST find some routines that can take HTML, and convert that HTML into valid PDF format.

    Login or Signup to reply.
  2.  byte[] bytes = e.strarr;
                     
      //write byte array to PDF 
             File.WriteAllBytes(context.Current.Server.MapPath("~\writedata\A.pdf)"),bytes);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search