PDF Viewer question

0
0

I’m using a PDF viewer set to Mozilla (assuming pdf.js will be used).

 

When I set the PDFSource property to some url (pdf file is on another web server) – I do not get anything – I get an unknown error in PDF.js window.

Is this a prpblem with PDF.js or is it how it is used in WiseJ ?

It should be able to view a PDF on another server from just a url correct ?

  • You must to post comments
0
0

No if the other site doesn’t allow CORS. It’s not  a Wisej or pdf.js issue. It’s how browsers enforce security. With Wisej you can load the external pdf from the server and return it to your viewer. If you have access to the other server you can enable CORS.

  • You must to post comments
0
0

So – is it the server side retrieving the pdf at the url or is it PDF.js ?

If I paste the URL in my browser directly it comes up.

  • You must to post comments
0
0

When you put the URL in the browser the document is download by your PC, not the browser (javascript). CORS is not involved. Pdf.js is a client widget. That’s why you can download the pdf from the server without incurring in CORS.

  • You must to post comments
0
0

So,  When I set the pdfsource property from within the code to a file on thedrive like this: “c:\test\test.pdf” then PDF.js can load it.

 

I need to send it from a url like this: images.ashx?image=abc.pdf for example

The URL is from the same site right now – trying to at least get it working first.

I wrote a handler that does this (.ashx within the same application):

// context.Response.ContentType = @”application/pdf”;
// context.Response.WriteFile(@”c:\test\test.pdf”);
// context.Response.End();

System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.ClearContent();
response.Clear();
response.ContentType = @”application/pdf”;
response.AppendHeader(“Content-Disposition”, “attachment; filename=test.pdf;”);
response.TransmitFile(@”c:\test\test.pdf”);
response.Flush();

 

But pdf.js gives an error: Invalid or corrupt pdf file:PDF.js v2.0.480 (build: a7a034d8)
Message: Invalid PDF structure

Do you have an example on how to properly stream a file to pdf.js from a url ? (I tried 2 methods above – Googled it for a while, looks like i’m doing it correctly)

 

  • You must to post comments
0
0

Even tried this:

setting the PdfSource = http://localhost:64730/images.ashx  (via a text box and button at run time)

 

My current .ashx handler:

try
{
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();

byte[] buffer;
using (FileStream fs = File.OpenRead(@”c:\test\test.pdf”))
{
int length = (int)fs.Length;

using (BinaryReader br = new BinaryReader(fs))
{
buffer = br.ReadBytes(length);
}
}

response.Buffer = true;
response.ContentType = “application/pdf”;
response.BinaryWrite(buffer);
response.Flush();
catch (Exception ex)
{
string ls = ex.Message;
throw;
}

If i put that URL in my browser it comes up and shows the PDF so its definitely sending it out- but nothing shows in PDF.js at all.

I think I’m about out of attempts. Anyone done anything similar ? I thought this last try would work.

 

  • You must to post comments
1
0

I don’t know what is wrong with the code. I can tell you that the PdfViewer in Wisej returns a stream and it works. This is the source code from Wisej, in case it helps you find the error. The first part sends back a pdf stream, the second a pdf file.

 /// <summary>
 /// Process the http request.
 /// </summary>
 /// <param name="context">The current <see cref="T:System.Web.HttpContext"/>.</param>
 void IWisejHandler.ProcessRequest(HttpContext context)
 {
 HttpRequest request = context.Request;
 HttpResponse response = context.Response;

 response.ContentType = "application/pdf";
 response.AppendHeader("Access-Control-Allow-Origin", "*");
 response.AppendHeader("Content-Disposition", new ContentDisposition() { DispositionType = "inline", FileName = Path.GetFileName(this.PdfSource) }.ToString());

 if (this._pdfStream != null)
 {
   try
   {
     if (this._pdfStream.CanRead)
     {
       try
       {
         this._pdfStream.Position = 0;
       }
       catch { }

       this._pdfStream.CopyTo(response.OutputStream, 1024);
     }
   }
   catch (Exception ex)
   {
     LogManager.Log(ex);
   }
 }
 else
 {
   try
   {
     string filePath = this.PdfSource;
     if (!Path.IsPathRooted(filePath))
       filePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, this.PdfSource);

     response.TransmitFile(filePath);
   }
   catch (Exception ex)
   {
     LogManager.Log(ex);
   }
 }
 response.Flush();
}

 

  • edmond girardi
    Okay – o after seeing this and modifying my code, this seems to work: try { string ls_filename = @”c:\test\test.pdf”; System.Web.HttpResponse response = System.Web.HttpContext.Current.Response; response.ContentType = “application/pdf”; response.AppendHeader(“Access-Control-Allow-Origin”, “*”); response.AppendHeader(“Content-Disposition”, new ContentDisposition() { DispositionType = “inline”, FileName = Path.GetFileName(ls_filename) }.ToString()); response.TransmitFile(ls_filename); } catch (Exception ex) { string ls = ex.Message; throw; } I did a test and commented out the line: response.AppendHeader(“Access-Control-Allow-Origin”, “*”); and it stops working. What is strange is that it’s not coming from another server or domain – its being served from the same application. So not sure what that’s needed
  • Luca (ITG)
    It makes sense – because pdf.js is loaded from a different origin. You wouldn’t need the Allow-Origin header if pdf.js was loaded from the same server as the web page.
  • edmond girardi
    Ok – sounds good – i’m going to try and load PDF.js from my local server and give it a try
  • edmond girardi
    I confirm this works correctly when I’m hosting PDF.JS myself on my server.
  • You must to post comments
Showing 6 results
Your Answer

Please first to submit.