Jump to content

Moved Server Now Cannot Generate Invoices


Recommended Posts

I cloned my test server by copying all the Prestashop files to the production server, imported a dump of the original database into the new database then updated all the URLs in the new database to point to the new URL.

 

I also deleted .htaccess then re-generated it via the backoffice then cleared the caches, thus taking care of the usual things that cause this kind of migration to fail.

 

Everything works beautifully (front office and back office) EXCEPT generation of .pdf invoices. Only the images (for example, the logo in the invoice header) are a problem. Some images cause an error others just don't appear in the generated invoice (all work fine in the test server).

 

At this point, I can't go live because after a user pays with PayPal and clicks the final button on the last page of checkout, when an invoice is generated a page with "TCPDF ERROR..." is returned to the user. Ouch!

 

Some (but not all) <img> tags in the smarty templates cause the error: TCPDF ERROR: [image] Unable to get image: ...etc. and no .pdf is generated. If I temporarily remove these tags, then some other images (ex. the logo in the header) just do not appear (but the .pdf is generated).

 

After 8 painful hours I have so far determined that .pdf templates are initially expanded to HTML which is parsed then converted to .pdf in tcpdf.php. All the URLs for images (<img...> tags) in the HTML template end up populated with absolute URLs (for example <img src="www.newsite.com/26-small_default/product.jpg" />) which are the "friendly name" URLs. These are fetched using curl and stored in a temp file. The URLs get rewritten (because of .htaccess) in the server to something like www.newsite.com/img/p/2/6/26-small_default.jpg which is where the actual image is located. I verified the images are there and I can get the images in a browser from another computer using an absolute URL (either the friendly or re-written URL works).

 

When the code in tcpdf.php issues a curl_exec to get the image (to a temp file in /cache/tcpdf), the server returns a 302 redirect with the header pointing to the same URL that was requested EXCEPT the server name is replaced with localhost.

 

For example,

www.newsite.com/26-small_default/product.jpg

becomes

http://localhost/26-small_default/product.jpg

 

The redirect is not normally followed so the operation fails (instead of an image, a small HTML file with "302 Found", etc. is saved in the temporary file and the failure occurs when later code tries to read the temporary image dimensions).

 

I temporarily added CURLOPT_FOLLOWLOCATION to the curl_exec and the redirection is followed but the generation of the .pdf still fails (I did not follow through to determine why it now fails-- I think the image is still not retrieved to the temp file).

 

A few other things I tried (on a fresh copy of the installation): delete the smarty compile directory, put various hard-coded URLs for the images in the smarty templates (invoice.tpl, header.tpl). I also checked the database and config files for any reference to the old URL and grepped all installed files for the old URL (none found).

 

Anyone have any idea why this is happening?

Link to comment
Share on other sites

I found the solution (answering my own question):

 

Quick answer: I did not yet set up public DNS for the new site's domain so downloading the image by curl failed. The solution was to put the new domain into the server's hosts file (or set up public DNS for the new domain).

 

(Suggestion to Prestashop: failure to download an image should not generate an error page that the customer sees, especially in the context of finalizing payment for a purchase. Maybe it's better to simply leave out the image in the .pdf or substitute an "image not found" image)

 

Long answer for those who might want to learn a little more:

 

To generate a .pdf invoice, an HTML version of the invoice is first generated (per invoice.tpl) which is then parsed and converted to .pdf by code in tcpdf.php.

 

When images are added to the .pdf file, they are downloaded (using curl) from the URL specified in the src attribute of the <img> tag in the HTML version of the invoice. They are NOT retrieved directly from the local file system (which is how images are retrieved in most, or all, other contexts-- which explains why this is the only place where I was having a problem).

 

I had not yet set up DNS for the new site's domain. Instead of failing the DNS lookup for the unknown domain, my ISP (Time Warner Cable) returns an IP address that generates an advertising page if it is requested by a browser. When the image is requested by curl from this IP address, it returns the 302 redirect that I saw. The redirect URL has the domain replaced by "localhost", but otherwise it is the same URL that was requested. Because the URL for the image is a "friendly name", it does not exist at the specified location. Normally, .htaccess handles the translation, but it only looks for the domain explicitly, not "localhost" so when I told curl to follow the redirection it still failed.

 

The only thing I still don't understand is why the logo image (in header.tpl) was simply left out, but an image in the body of the invoice caused the ugly "TCPDF ERROR"

Link to comment
Share on other sites

  • 1 year later...

I am having the same problem. Site is a clone of production site on local server and generates the same TCPDF error ("TCPDF ERROR: [image] Unable to get image: /var/www/my_site/cache/tcpdf/img_oardqY"). The clone site's domain name is in my hosts file. I cannot configure the domain in DNS because it's a test server and does not have a registered domain name. Database was updated with new URLs, all caches, etc. have all been cleared and .htaccess regenerated. Everything else works perfectly except for invoice generation. I checked all prestashop files for hard-coded domain name and there are none.

 

The temporary image file that is reported as missing is not on the server. The .../cache/tcpdf directory is empty. Permissions allow read/write access to the directory (in fact, since this is a test server I set permissions for ALL directories on the server to "xrw" (777) and it still does not work.

 

I would appreciate any suggestions. This is a showstopper because I can't have that error appear when a customer is finalizing payment.

Link to comment
Share on other sites

Answered my own question.

 

I feel kind of stupid, but I will "fess up" and admit my dumb mistake. I added the clone site to hosts, but I did it on the machine where I was accessing the site from my web browser and not on the web server. DUH!

 

So, problem solved

 

Thanks and apologies to anyone who looked at this.

Link to comment
Share on other sites

×
×
  • Create New...