Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating PDF between 2 sites

I'm standing infront of a crossroad full of potential ways to solve my problem. My problem is that I want to refer specific users to a unique page that renders relevant PDF file for them. Preferably, I want to refer them to one site, which the relevant MySQL-data and plugin for pdf rendering isn't on. Hopefully, you can shed some light to which solution I should use.

Reference

  • Site A - The site I prefer to refer them to be on
  • Site B - The site containing the MySQL data and PDF plugin

Info

  • Both sites are built through CakePHP 1.3
  • Site A currently doesn't have a database attached to it. It simply runs on API calls only to Site B.
  • These PDFs won't be that heavy to generate, though it would be interesting to find a solution which also accounts for this
  • The reference to these PDFs occurs upon events, and are not sent to thousands of users at the same time.

Solution #1

Refer them to Site B and generate everything there.

Pros: Easy to fix.
Cons: I don't want these users to know of Site B. Site B is intended mostly for internal communication, and would be best let out of this for given users.
Thoughs: Rather avoid this one.

Solution #2

Have a page on Site A that cURLs a page equal to Solution #1, and then outputs the same result.

Pros: Pretty much equal easy to fix.
Cons: Can't think of any.
Thoughs: Will the browser understand that I output a PDF? Or do I (if possible) copy headers reply from cURL request and set them in own header() before output?

Solution #3

Generate the PDF once in Site B and place it on Site A. Then simply refer to the .pdf link.

Pros: Faster loading. Not that it matters in this case and might even go unnoticed.
Cons: Can't as easily modify the PDF output.
Thoughs: How would I transfer the file? The 2 sites are on the same server, so it would be possible with a simply path change, however all other communication between the sites are made to that they don't need to share servers. Shame to break that design. Maybe I would have to do an advanced cURL request and send the pdf file as a POST from Site B to Site A and upload it? Doesn't seem as neat solution either, though.

Solution #4

Run an API from Site A to Site B to get relevant data based on ID from url. But also have the PDF plugin on Site A aswell.

Pros: In one way, quite a logical approach.
Cons: I would prefer to have all PDF generating on Site B only. Makes it easier to manage all of them.
Thoughs: I'm a bit unsure how much (if any) more beneficial this approach would be compared to Solution 2.

Many thanks for your time. Please motivate one of given solutions, or present your own one.

EDIT: Although code samples always are appreciated, I'm more interrested in the resoning and logic to why which one of the solutions, or an other, should be used. I already know how to solve most of these solution through coding. For visitors, feel free to link to relevant functions and methods regarding your reply.

like image 267
Robin Castlin Avatar asked Nov 12 '22 16:11

Robin Castlin


1 Answers

It looks like you want to hide Site B from the public eye.

The easiest way is to create a reverse proxy between Site A and Site B. You can do something like sitea.com/pdf-items/ will serve siteb.com/

EDIT:

The reverse proxy can be cross domain, the servers do not have to share anything in common, except for Site B being accessible by Site A (which sounds like it is already)

Apache has a simple approach using mod_proxy (http://httpd.apache.org/docs/2.2/mod/mod_proxy.html)

A quick google pulls up a guide for setting it up on Apache. http://www.apachetutor.org/admin/reverseproxies

Nginx has one too http://www.cyberciti.biz/tips/using-nginx-as-reverse-proxy.html

IIS is a bit more complicated and I've never set it up, but the ability does exist according to the documentation.

EDIT 2: Approaching

Here we don't have to worry about PHP component. What happens is that the Apache server will map forward the request based on the proxy specified e.g. if Proxy is set Sitea.com/pdf siteb.com/ then sitea.com/pdf/alpha.pdf will actually request siteb.com/alpha.pdf . In this schema it will ignore the sitea.com PHP routing all together, but will honor siteb.com routing as it is a full-fledged request but done by sitea's webserver.

In regards to honoring, the requests for siteb, it implies that sitea.com/pdf/getpdf.php?id=1 will actually go through all motions as going to sitea.com/getpdf.php?id=1.

Alternatively if you want to setup a VHOST on siteA.com such as pdf.sitea.com you can setup the proxy that pdf.sitea.com maps to siteb.com, but this would be useless if both sitea and siteb are publicly accessible.

The reverse proxy works best if sitea.com is available to your audience and siteB is behind a firewall that has restricted access, so the proxy will let sitea.com visit a section of siteb.com that otherwise would not be accessible.

Files that would be modified are siteA.com Apache configuration for the host by enabling the proxy_mod and setting up the ProxyPass and ProxyReversePass under the VHOST or Server configuration.

like image 131
Ikstar Avatar answered Nov 15 '22 05:11

Ikstar