1

I have the following script which is used to PDF some pages on a website:

<? 
    include_once('phpToPDF.php') ; 
    phptopdf_url( 'http://www.example.com/csb04-termination-box/?style=alt' ,'pdf/', 'csb04-termination-box.pdf'); 
    phptopdf_url( 'http://www.example.com/csb05-termination-box/?style=alt' ,'pdf/', 'csb05-termination-box.pdf');  
    phptopdf_url( 'http://www.example.com/csb06-compact-termination-box/?style=alt' ,'pdf/', 'csb06-compact-termination-box.pdf');
    echo 'Done';
?>

The problem I have is if I add many more pages the server times out before the script has finished running. I've tried changing set_time_limit(0); but that hasn't helped (on shared hosting).

What would be the best way to amend the script so it can finish? I considered trying to split it in to multiple scripts and running it via ajax but not sure where to start with that as have no prior experience with it.

2 Answers 2

3

The easiest way to do it would be this :

include_once('phpToPDF.php') ; 
$i = isset($_GET['i']) ? intval($_GET['i']) : 0;

if ($i == 0) phptopdf_url( 'http://www.example.com/csb04-termination-box/?style=alt' ,'pdf/', 'csb04-termination-box.pdf'); 
if ($i == 1) phptopdf_url( 'http://www.example.com/csb05-termination-box/?style=alt' ,'pdf/', 'csb05-termination-box.pdf');  
if ($i == 2) phptopdf_url( 'http://www.example.com/csb06-compact-termination-box/?style=alt' ,'pdf/', 'csb06-compact-termination-box.pdf');

if ($i < 3) header('Location: ?i=' . ($i + 1));
echo 'Done';

Basically this lets your webbrowser do the counting, without using Ajax. When one request is done, the browser automatically loads the next page, until the counter ($i) is at 3.

Sign up to request clarification or add additional context in comments.

3 Comments

Is there a way to include an on screen update of progress easily with this? Just like an echo'1/3' after processing the first one and so on...
aha.... just come across a problem with this.. "Error 310 (net::ERR_TOO_MANY_REDIRECTS): There were too many redirects."
You could probably try using JavaScript redirects (<script>window.location='?i=1';</script>) or HTML redirects (<meta http-equiv="refresh" content="1;url=?i=1">), which both let you output data to the screen.
0

Use the following script to process all your files, one at a time:

include_once('phpToPDF.php') ; 

$urls = array();
$urls[] = array('http://www.example.com/csb04-termination-box/?style=alt', 'csb04-termination-box.pdf');
$urls[] = array('http://www.example.com/csb05-termination-box/?style=alt', 'csb05-termination-box.pdf');
$urls[] = array('http://www.example.com/csb06-termination-box/?style=alt', 'csb06-termination-box.pdf');
// add here more elements in $urls if needed

if(!isset($_GET['n']))
    $n = 0;
else
    $n = intval($_GET['n']);

if(isset($urls[$n]))
{
    phptopdf_url($urls[$n][0] ,'pdf/', $urls[$n][1]); 
    header('Location: ?n='.($n+1)); // calls the conversion of the next file in the $urls array
}
else
    echo 'Done';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.