How to open a pdf file in a new browser window in javascript

I need to open a pdf file in a new browser tab. How to do it. I used

var docLocation = '../downloads/doc.pdf';
window.open(docLocation,"resizeable,scrollbar"); 

But it opens a browser download dialog. How to achieve this?

+5
source share
5 answers

The ability to display pdf depends entirely on whether the user has a plug-in for displaying pdf, and also has its own settings for processing PDF files in this way.

There are some flash widgets that can be used to present PDF content to the user, but for a direct answer to your question you cannot control user preferences as they chose to process PDF files.

+4
source

here

    <a href="javascript:void(0);" onclick="javascipt:window.open('YourPDF.pdf');" class="popup">Clic to open.</a>

+2

, Content-Type "application/pdf", "application/octet-stream"

+2

pdf JavaScript

var pdf = MyPdf.pdf;
window.open(pdf);

:

function openPDF(pdf){
  window.open(pdf);
  return false;
}
+2

, , javascript mvc 3, , adobe 11, , , Chrome Firefox. , .

PDF-, javascript,

:

    var URL_OPEN_REPORT_PDF                   = "@Url.Content("~/Report/OpenPDF/")";

JavaScript:

    var sURL = URL_OPEN_REPORT_PDF;
    sURL = AddURLParameter(sURL, "ReportArchive", moControl.treeOrganization.getUserData(sItemUI, "reportarchive"));
    window.open(sURL);

ReportController.cs:

    [Authorize]
    [HttpGet]
    public ActionResult OpenPDF(string ReportArchive)
    {
        PDFResult oPdfResult = new PDFResult();

        ReportArchive oReportArchive;

        var serializer = new JavaScriptSerializer();
        oReportArchive = serializer.Deserialize<ReportArchive>(ReportArchive);
        string FilePath = Server.MapPath(string.Format("~/Content/Reports/{0}", oReportArchive.FileName));

        WebClient User = new WebClient();

        Byte[] FileBuffer = User.DownloadData(FilePath);

        if (FileBuffer != null)
        {
            oPdfResult.Length = FileBuffer.LongLength;
            oPdfResult.FileBuffer = FileBuffer;
            Response.BinaryWrite(FileBuffer);

        } return View("PDF", oPdfResult);
    }

ViewModel PDFResult.cs:

public class PDFResult
{
    /// <summary>
    /// Content Length
    /// </summary>
    public long Length { get; set; }

    /// <summary>
    /// Content bytes
    /// </summary>
    public Byte[] FileBuffer { get; set; }
}

View PDF.cshtml file:

@model Report.PDFResult
@{
    Response.ContentType = "application/pdf";
    Response.AddHeader("content-length", Model.Length.ToString()); 
    Layout = null;
}
0
source

All Articles