Abcpdf 8 with ASP.Net - Missing images in PDF for some users - asp.net

We're using ABCPDF.Net 8 to generate PDF documents for users. The HTML source is loaded directly as a string and images are defined in the HTML with the full path. Example:
<img id="img1" src="file://D:/trakit/images/cl_logo.gif" alt="" />
This works beautifully and we have about 200 users generating PDFs with no problem.
The problem is that a few handful of users are not getting the images in the PDF. The images are replaced by the image placeholder that is shown by IE when an image is not found.
This happens consistently for some users only. I cannot figure out what is wrong as the PDF is generated server-side and should not have anything to do with the user's browser settings. I agree that the page/code is running under the user's permissions (we use windows authentication), but why should this affect PDF image generation? Below is the code snippet to generate the PDF:
PDFDoc.Rect.Inset(20, 20)
PDFDoc.Page = PDFDoc.AddPage()
li_PDFID = PDFDoc.AddImageHtml(as_HTML, True, 1050, True)
While PDFDoc.Chainable(li_PDFID)
PDFDoc.Page = PDFDoc.AddPage()
li_PDFID = PDFDoc.AddImageToChain(li_PDFID)
End While
For li_Loop = 1 To PDFDoc.PageCount
PDFDoc.PageNumber = li_Loop
PDFDoc.Flatten()
Next
Return PDFDoc.GetData()
Any help will be greatly appreciated.
Thanks.
Update: Just found that if the user users another browser (such as Firefox), the images then generate fine. This narrows down the problem to IE 7/8 on the user's computer. Any suggestions?

Okay. The issue was finally found and has nothing to do with ABCPDF.
Seems part of the HTML source was coming from the user's browser - obtained using javascript function innerhtml() on IE. And I just found out that IE has a big problem with innerhtml()
All sorted now.

Related

PDF rendering issues on IE 11

All, I am working on an ASP.NET 4.6.1 web forms application that renders pdf documents natively on a browser.I get an error message only on IE 11 when some pdf documents are rendered as shown in the screenshot saying "The file is damaged and could not be repaired.Local\EWH-6624-0".The same document renders fine on Chrome and FireFox.Has anyone encountered the same issue? I downloaded the same pdf file in Chrome and tried to open this in Adobe Reader version 11.0.22, it gives me the "There was an error opening this document."The file is damaged and could not be opened".Please see the screen shots below
The asp.net application gets the data from a service and renders it on the UI.This is the C# code that does this
var data = getdataAndOtherThingsFromService();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", data.DocumentStream.Length.ToString());
Response.BinaryWrite(data.DocumentStream);
IE isn't rendering the PDF. As the dialog clearly indicates, the Adobe Reader plugin is attempting to render the PDF but is unable to. That's why you get the same dialog when you download the file and open it.
Chrome, Firefox, and Edge, and even PDF files hosted on Dropbox have their own PDF rendering engines built-in and, apparently, are more forgiving of badly formatted PDF than Adobe Reader is.
Unfortunately, you will never be able to create a consistent experience if you rely on the browser, or browser plugins to render your PDF files. Instead, you'll need to implement something like PDF.js which, while not a perfect PDF rendering tool, will at least behave predictably across browsers and operating systems.
Adding the code below that fixed my issue.This might be helpful to someone in the future
var data = getdataAndOtherThingsFromService();
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", data.DocumentStream.Length.ToString());
Response.BinaryWrite(data.DocumentStream);
Response.End();

How to use PDF open parameter (e.g. fitH) in Chrome/ FireFox browser?

http://www.adobe.com/content/dam/Adobe/en/devnet/acrobat/pdfs/pdf_open_parameters.pdf
There are a set of open parameter specify how the viewer should open the pdf . However, with the Chrome and firefox browser's default viewer , it does not work
<object id = 'zoomPage' type='application/pdf' data= '1.pdf#view=fit'><p>The PDF can not display</p></object>
I have tried the fit which I would like the pdf fill the object. If there is no parameter support , are there any workaround ? I tried a lot of approach but none of them work. For example, I also tried using iframe instead of object. However, the content does not resize. Thanks
<iframe src="1.pdf" width=1300 height=3300></iframe>
Searching a solution for a problem very similar to yours, I found an interesting thread on forum.asp.net. http://forums.asp.net/t/1877403.aspx?Issue+with+embedded+pdf+object+in+chrome+browsers
At the end of the page, there is a workaround by a user working smoothly in Chrome. That seems to solve the problem.
Here it is an example:
<iframe src="/Downloads/MyPdfDocument.pdf#view=fitH" width="700" height="880"></iframe>
For users fighting with http://pdfobject.com/ -> "pdfobject.js" like me, you can change few lines of code on both minified and develop version of pdfobject.
Find:
c.innerHTML='<object data="'+a+'" type="application/pdf" width="'+i+'" height="'+z+'"></object>';return c.getElementsByTagName("object")[0];
and change with this line of code:
c.innerHTML='<iframe src="'+a+'" width="'+i+'" height="'+z+'"></iframe>';return c.getElementsByTagName("iframe")[0];
Hope this helps.
This post is quite old, but nowadays you can put #view=fit on the end of an iframe's src URL to make sure the PDF opens a page to fit within the iframe.

Chrome caching CSS but not loading images inside CSS file

We're having a weird problem at work that happens only in chrome. It looks like the css file is getting cached and the content of this file isn't getting re-downloaded.
The problem is that when using a fresh session for example "private session", the image "mainSprite.png" isn't getting displayed.
After some tests, I believe the problem is related to us doing redirects at the beginning if the user isn't authenticated. From what I understand, it might not complete the download of the sprites linked inside the css files. It will cache an invalid object as soon as the redirect starts and then on the following pages, it will fail to display a correct image since it cached something wrong.
The strange thing is that it actually loads the image completely at some point. But it looks like it's not refreshing it in memory...
I did a timeout of one second before starting redirects on first load and images correctly display. This is a quick fix and I can't expect every computer to load in 1 second every images contained in the css.
edit
As far as I can say, it really looks like a race condition. I changed the order of loading. We use require.js. Instead of loading js after css, I start js loading before. And images are getting loaded correctly now on my local server.
if someone is interested to look into it:
http://api.checklist.com
edit 2
When images aren't visible, opening new tabs will have the same problem. Closing the browser and reopening it will work on first load and images isn't being downloaded but loaded from Cache which means that before closing the browser, the image was indeed downloaded.
It looks like the problem coming from your redirects unfortunately i couldn't see your example ( link won't open ). Google chrome has indeed issues with caching it's annoying during development time ( clear up the cache, load new image, do the same for new image..), if you need to clear your cache try the folowing:
try to go to
chrome://chrome/settings/clearBrowserData
in your chrome browser and check the options:
Empty the Cache( i have also Clear download history and Delete cookies and other site and plug-in data )
click on 'Clear Browsing Data' button it should
All what you need to do is to trace your cash list via chrome, and from what I see is that you got this error which make it not cached:
Uncaught TypeError: Object [object Object] has no method 'placeholder'
So if you want to trace, you can use the manifest offline mode or you trace via your code.
Just following and test your page, I did catch where the error is:
file: scripts2.js Line 20 --> $('input[placeholder]').placeholder();
which you need to check the name of the place holder and change it here in this tag.
Thank you
I assume your server/backend app has routes set up. Like this Play! framework example:
# Ignore favicon requests
GET /favicon.ico 404
# Map static resources from the /app/public folder to the /public path
GET /public/ staticDir:public
# Catch all
* /{controller} {controller}.index
According your summary I suggest to set up a static folder route (where the images are) in config file or htaccess as you want, then check image url in browser url bar (with empty session). That should work!
First I would suggest that you first try to find ways to narrow the redirects. If it possible I would suggest that it would be much more advisable to try to create your content dynamically based on your users authentication using languages like PHP or ASP (just to name two).
The classic way of disabling the caching on a webpage is to set two <meta> tags inside of your <head> </head> tags. However, you should note that these are technically not "correct" as they are not part of any of the "offical" standards documentation. This also means that I would again lean towards my first suggestion of finding a better delivery system which in turn should prevent the problem.
So for "testing" purposes the tags would be:
<HEAD>
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<META HTTP-EQUIV="EXPIRES" CONTENT="0">
</HEAD>
Maybe I don't understand your question or dilemma (maybe because of lack of explanation or because I can't see your page at that link since I run Chrome), but there's an example I ran across here that works in Chrome by just using Javascript/jQuery to load, instead of CSS:
http://jsfiddle.net/2Cgyg/6/
Use image at URL: http://www.w3schools.com/cssref/img_tree.gif
And although the accepted answer didn't work for me in Chrome, this is the question I got the jsFiddle above, from:
Load Image from javascript
All the caching, etc. is unnecessary, and even something you wouldn't want to do if your images are ever updated to something else - they won't appear without forcing a refresh which you can only do through altering the file name like this to avoid users not seeing your updated image:
myPic.jpg?MMDDYYYY
And you could set the date according to the date you are modifying it.
clean your browser history like cache,cookies
clean the temporary internet file
if problem not solved then reinstall browser your problem is solved definitely

Outputting Images in ASP FPDF problem

Hi
I'm attempting to use the ASPFPDF class to output an image on a pdf using the very simple code below:
<!--#include file="fpdf.asp"-->
<%
Dim pages
Dim pdf
Set pdf=CreateJsObject("FPDF")
pdf.CreatePDF "P", "mm", "A4"
pdf.SetPath "fpdf/"
pdf.Open()
pdf.AddPage("P")
pdf.Image "invoiceheader.jpg"
pdf.Output ()
%>
When run, this script should open the pdf but with Adobe Reader I get a "File does not begin with %pdf" message. I've tried using other readers with similar results.
If I remove the image line and output some text (e.g. pdf.Cell 40,5,"Date",0,1,"R"), everything works perfectly.
Anyone got any ideas before I go mad!
Thank you.
Try this one
pdf.Image "IMAGES/invoiceheader.jpg", 140, 22, 50, 40
Here 140 is the x position, 22 is the y-position 50 is the width and 40 is the height.
I don't know AspFPdf, but don't you need to specify a folder where the image is to be found ?
Did you try ?
pdf.Image "fpdf/invoiceheader.jpg"
As I said, I don't know the component, just my thoughts (although you have propably tried this yourself).
[EDIT] Look at the website of Asp Fpdf and saw the following in the documentation:
file
Path or URL of the image.
Asp fPdf : Image parameter
Anyway: thanks for pointing me in the direction of this component (even though this was not your intention). I can use this too ;-)
Just thought I'd update people as I've spent ages and have finally got this working. What you don't see due to random error messages is that the FPDF application creates a temporary PDF as it's building it. By default, it will create it in the same folder as your asp page that runs it.
e.g. My page was in my web root \createpdf.asp
I worked out that it was trying to create a file called D10456.tmp (name changes each time) in my web root but it couldn't due to security.
I added an output file name and changed the security in another folder to allow write access and it worked first time.
Code for output file:
pdf.Output server.mappath("pdfs\list.pdf"),"F"
The PDFS folder must have write access to work.
Hope that helps people.
The answer by Paul is also correct for the format of adding an image in ASP.
Also, I had to change the images.asp file which is located in the includes directory.
I had to change the line:
this.Buffer.LoadFromFile(Server.MapPath("\\") + ("\\") + ("Images") + ("\\") + pFileName);
to
this.Buffer.LoadFromFile(Server.MapPath(pFileName));
because it was looking in the wrong folder for my images.
The answer to the above question is that the FPDF seems to need to be on a fully qualified domain - I was running on a machine with using a custom hosts file to point the URL at the development server. As soon as I moved the code to the live server, everything worked!
I don't fully understand why, but this might help someone else...
I found that the only way I could add images was as follows:-
pdf.Image "./byimage.jpg",x,y,width
This is how i manage to display my image in ASP classic page:
pdf.Image "fpdf/header.jpg", 10, 10,80,20, "jpg"
if not mistakes, this is how to define the variables (correct me if i'm wrong):
pdf.image "folder/image.type", x-coordinate, y-coordinate, img width, img height, "image type"
hope it helps. because i've spent many days just to find how to display the image in my asp classic page..

ASP.NET Build Images Links Dynamically

I am developing a website that has product images on an external server. I have code that tests to see if the image exists like (pseudo code):
DynamicString = FunctionThatCreatesDynamicString()
' DynamicString = "http://external_server/path/to/file1.jpg"
If ImageExists(DyanmicString) = StatusCode.200 Then
' Embed link in ASP.NET page
Else
' Embed not found image in ASP.NET page
End If
My code builds fine and appears to execute. The problem occurs when I attempt to view the external link in a browser, the image appears properly (I have to authenticate first, but that's OK considering I'm on an internal network and this app will be used internally).
However, when I attempt the view the source in my generated HTML page, I am seeing the image to the "Not Found" image when I know the image is there.
I compared all the characters in my dynamically assembled to the external link and all the characters are matching up correctly.
I'm wondering if the authentication has anything to do with why the image is not rendering properly on my rendered HTML.
Any thoughts?
It turns out the problem was the authentication to the images. I tried and was able to use a completely different approach. But thanks for the tip tangurena.

Resources