This is an archived forum post. The information may be outdated. Contact us if you have any questions.

Pdfcrowd Error: [503] Simultaneous API calls from a single IP are not allowed.

clj83 wrote on 2013-01-04:
Hi,

I have just setup the HTML to PDF API for PHP and it is most excellent. However, I am getting a 503 error, Simultaneous API calls from a single IP are not allowed. I don't want to be sending simultanious calls, just one. Could you look at the code below and tell me what I am doing wrong?

require 'pdfcrowd.php';

try
{   
    // build the url and remove the pdf field from the query string
    $url = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"];
        if (count($_GET) > 1) {
            unset($_GET["pdf"]);
            $url = $url . "?" . http_build_query($_GET, '', '&');
        }

    // call the API
    $client = new Pdfcrowd("***", "***");
    $pdf = $client->convertURI($url);

    // send the generated pdf to the browser
    header("Content-Type: application/pdf");
    header("Cache-Control: no-cache");
    header("Accept-Ranges: none");
    header("Content-Disposition: attachment; filename=\"Invoice-ID-$filename.pdf\"");
        
    echo $pdf;
}
catch(PdfcrowdException $why)
{
echo "Pdfcrowd Error: " . $why;
}
support wrote on 2013-01-05:
Hi,

please post here a link (a URL) that calls the API and we will look into it.
clj83 wrote on 2013-01-08:
http://demo.group74.com/index.php?page=showinv&idi=MjkwMDAwOzMwMDAwOzI0NTYyNjM=&showtemplate=false

Thanks
support wrote on 2013-01-09:
It seems that you are calling the API recursively and the second request fails with the 503 error. Here is an explanation:

1/ A user clicks the PDF generation link.
2/ Your code calls the API with $url.
3/ The API visits the passed URL which in turn executes your code again.
4/ Your code calls the API the second time with $url but this time the call fails with 503 since there already is a pending API request.

To resolve this just add the "pdf" field to the PDF generation link and check for its presence.

http://demo.group74.com/index.php?page=showinv&idi=MjkwMDAwOzMwMDAwOzI0NTYyNjM=&showtemplate=false&pdf=1

If the "pdf" field is set remove it from the URL and call the API. Otherwise render your page as usual:
if ($_GET["pdf"])
{  
    try
    {   
        // remove the pdf field from the url
        $url = "http://" . $_SERVER["SERVER_NAME"] . $_SERVER["PHP_SELF"];
        if (count($_GET) > 1) {
            unset($_GET["pdf"]);
            $url = $url . "?" . http_build_query($_GET, '', '&');
        }

        // call the API
        $client = new Pdfcrowd("***", "***");
        $pdf = $client->convertURI($url);
        
        // send the generated pdf to the browser
        header("Content-Type: application/pdf");
        header("Cache-Control: no-cache");
        header("Accept-Ranges: none");
        header("Content-Disposition: attachment; filename=\"Invoice-ID-$filename.pdf\"");
        
        echo $pdf;
    }
    catch(PdfcrowdException $why)
    {
        echo "Pdfcrowd Error: " . $why;
    }

    // do not render the HTML page, stop here
    return; 
} 
else 
{
    // render your HTML page here ...
}
tuanpa wrote on 2013-10-18:
n/a