Hello, I am using a php function to merge 2 pdfs and it works great.

<?php
include 'PDFMerger.php';

$pdf = new PDFMerger;

$pdf->addPDF('testFile5.pdf', 'all')
    ->addPDF('testFile57.pdf', 'all')



    ->merge('download', 'samplepdfs/TEST2.pdf');
    ?>

I get a merged pdf as soon as the page loads.
I need the ability to make this merge happen when the user clicks a button, and I am stumped.

Im a bit of a php nooby so please excuse my ignorance.

Further more I need the user to be able to select or click and drag over pdfs to be merged.

Any help is MORE than appreciated.

Recommended Answers

All 19 Replies

How are you stumped? What did you try?
If you use a select menu for the choosing of the .pdfs to be merged, and a submitted form to carry the choices to a processing function it's relatively simple.
You need to use javascript for the drag and drop, and to update the form variables before posting, it's certainly more complicated.

Actually Im working on combining the drag and drop Click Here but it is throwing an error that I am stumped on.
Im using this drag and drop Click Here

I could post the source code if you think it would help.

Ok, I don't know if I can help but posting the code can only help. I can see from the error 'Could not locate PDF on ''', and addPDF('', 'all') that the pdf to add variable is not assigned.

Do you know how I would assign that? I am a PHP novice at best.
Here is the source.

<?php
include 'PDFMerger.php';
$files = explode(",", $_POST['postOrder']); //or your name of the field

$pdf = new PDFMerger;

foreach ($files as $file) {
    $pdf->addPDF($file, 'all'); 
}

$pdf->merge('download', 'samplepdfs/TEST2.pdf');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>jQuery Examples from SKFox.com</title>

<link rel="stylesheet" type="text/css" href="http://skfox.com/jqExamples/jq14_jqui172_find_bug/jq132/css/ui-lightness/jquery-ui-1.7.2.custom.css" />
<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script language="JavaScript" type="text/javascript">

    $(function() {
        $("ul.droptrue").sortable({
            connectWith: 'ul',
            opacity: 0.6,
            update : updatePostOrder
        });

        $("#sortable1, #sortable2").disableSelection();
        $("#sortable1, #sortable2").css('minHeight',$("#sortable1").height()+"px");
        updatePostOrder();
    });

    function updatePostOrder() { 
        var arr = [];
      $("#sortable2 li").each(function(){
        arr.push($(this).attr('id'));
      });
      $('#postOrder').val(arr.join(','));
  }

</script>
<style type="text/css">
.listBlock {float: left;}
#sortable1, #sortable2 { list-style-type: none; margin: 0; padding: 0; margin-right: 100px; background: #eee; padding: 5px; width: 300px; border: 1px solid black;}
#sortable1 li, #sortable2 li{ cursor: move; margin: 5px; padding: 5px; font-size: 1.2em; width: 250px;  background: none; background-color: white;}
</style>
</head>
<body>
<div id="maincontainer">
  <div id="contentwrapper">
    <div id="maincolumn">
      <div class="text">
                <h2>&nbsp;</h2>
                <hr />
                    <div class="listBlock">
                        <h2>Available to print</h2>
                        <ul id="sortable1" class='droptrue'>
                            <li class="ui-state-default" id="testFile57.pdf">testFile57.pdf</li>
                            <li class="ui-state-default" id="testFile5.pdf">testFile5.pdf</li>
                            <li class="ui-state-default" id="testFile57.pdf">testFile57.pdf</li>
                        </ul>
                    </div>
                    <div class="listBlock">          
                        <h2>Send to print, in order</h2>
                        <li class="ui-state-default" id="testFile57.pdf">testFile57.pdf</li>
                        </ul>
                    </div>
                    <br clear="both" />



                    <form method="post" action="print_r($_POST)"> 
<input type="text" id="postOrder" name="postOrder" value="" size="30">
<p>Which articles, in which order?: <br /> 
<br /> *Normally this field would be hidden</p>
<input type="submit" value="submit" name="submit">
</form>


      </div>
    </div>
  </div>

</div>
</body>
</html>

On line 75 the action of the form is malformed.
You have duplicate id's on the list elements.
When the form is submitted, you need to check the variables exist and are in the correct format before you use the merge function.

Do you know how line 75 should read? Should it be <?php echo $PHP_SELF;?>?

Member Avatar for LastMitch

@double_cola

Wow, you are combining 2 different scripts into one? That's alot of work.

Member Avatar for LastMitch

@double_cola

<form method="post" action="print_r($_POST)">

When you post anything it should be in a file that show the result. When you post the same results in the same file it will read. But if you used print_r($_POST) into the same file it will not read it. So you can't used print_r($_POST) in the action" "
You need to post it in a different file.

It's much easier just to keep php rather than using jquery combination.

Hey LastMitch can you show me an example of what your talking about?

Member Avatar for LastMitch

@double_cola

For example (this is a simple form):

<html>
<body>
<form action="demo.php" method="post">
<input type="text" name="stuff" /><br /><br />
<input type="submit" value="Submit">
</form>
</body>
</html>

This is "demo.php"

<?php echo
$stuff = $_POST['stuff'];
?>

I hope you get the idea now! This is how you post a data and results to another file!

I'm not sure why you are using this:

<form method="post" action="print_r($_POST)">

it's not going to work.

Well that did it! We are almost there.
It is working correctly and it will save the pdf if you only drag 1 into the box and hit submit Click Here BUt if you try and put both in the box it doesnt work correctly.
Any ideas??

Member Avatar for LastMitch

@double_cola

You mean you change the

<form method="post" action="print_r($_POST)">

to a file?

That's good to hear! Try to create another <input> to store the second file.

Another words another <input>.

So there will be 2 <input>. I think that will do the trick.

First of all you guys are geniuses. LastMitch and Adam, all my internet points go to you :) !!
One last question. Can you think of any way to get this to work with only 1 drop box or 1 input? Potentially I could have 25 pdfs to choose from.

Member Avatar for LastMitch

@double_cola

Can you think of any way to get this to work with only 1 drop box or 1 input? Potentially I could have 25 pdfs to choose from.

I think that more JQuery than PHP because the drag and drop is from JQuery and I'm not familiar with JQuery. I don't think I can help you anymore with that one.

It's getting late, it's midnight!

If you have any other questions beside that one that you ask above post it here other wise Mark Solve for this Thread so people who has similar question like yours can always come here and see the correct answer! I hope you learn a lot today. I think you done a lot too.
That's a lot pdf files!

Adam, you are the man. And LastMitch you are also the man.
You both get all my internet points and you have acquired massive karma!
Im going to post all the final source incase anyone else has the same issue.

Here is the final outcome for a PDF merger user interface using PDFMerger Click Here and a jquery drag and drop by skfox.com Click Here

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>let the minds be blown</title>


<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script language="JavaScript" type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
<script language="JavaScript" type="text/javascript">

    $(function() {
        $("ul.droptrue").sortable({
            connectWith: 'ul',
            opacity: 0.6,
            update : updatePostOrder
        });

        $("#sortable1, #sortable2, #sortable3").disableSelection();

        updatePostOrder();
    });

    function updatePostOrder() { 
        var arr = [];
      $("#sortable2 li").each(function(){
        arr.push($(this).attr('id'));
      });
      $('#postOrder').val(arr.join(','));

        var arr = [];
      $("#sortable3 li").each(function(){
        arr.push($(this).attr('id'));
      });
      $('#postOrder2').val(arr.join(','));
  }

</script>
<style type="text/css">
.listBlock {float: left;}
#sortable1  { list-style-type: none; margin: 0; padding: 0; margin-right: 100px; background: #eee; padding: 5px; width: 300px; border: 1px solid black;}
#sortable2 , #sortable3 {
    list-style-type: none;
    padding: 0;
    margin-right: 100px;
    background: #eee;
    padding: 5px;
    width: 300px;
    border: 1px solid black;
    height: 45px;
    margin-top: 0;
    margin-bottom: 0;
    margin-left: 0;
}
#sortable1 li, #sortable2 li, #sortable3 li{ cursor: move; margin: 5px; padding: 5px; font-size: 1.2em; width: 250px;  background: none; background-color: white;}
</style>
</head>
<body>
<div id="maincontainer">
  <div id="contentwrapper">
    <div id="maincolumn">
      <div class="text">
<h2>Meet the amazing PDF generator. This is still a little abstract so picture the final result in your brain box.<br />
  Drag out the pdfs and place one in each of the send to print boxes, they will render out into 1 master file but in the order you place them.<br />
  <br />
  Let your minds...be blown!
</h2>
                <hr />
                    <div class="listBlock">
                        <h2>Available to print</h2>
                      <ul id="sortable1" class='droptrue'>
                            <li class="ui-state-default" id="testFile5.pdf">testFile57.pdf</li>
                            <li class="ui-state-default" id="testFile57.pdf">testFile5.pdf</li>

                        </ul>
                    </div>
                    <div class="listBlock">          
                        <h2>Send to print 1, ONLY 1</h2>
                      <ul id="sortable2" class='droptrue'>

                        </ul>
                    </div>
                    <div class="listBlock">          
                        <h2>Send to print 2, ONLY 1</h2>
                      <ul id="sortable3" class='droptrue'>

                        </ul>
                    </div>
                    <br clear="both" />





<form action="testit.php" method="post">
<input type="text" id="postOrder" name="postOrder" value="" size="30">
<input type="text" id="postOrder2" name="postOrder2" value="" size="30"><br /><br />
<input type="submit" value="Submit">


      </div>
    </div>
  </div>

</div>
</body>
</html>

And this is the page it is posting to.

<?php
include 'PDFMerger.php';
$goods = $_POST['postOrder'];
$goods2 = $_POST['postOrder2'];
$pdf = new PDFMerger;
$pdf->addPDF($goods, 'all')
->addPDF($goods2, 'all');



    $pdf->merge('download', 'samplepdfs/TEST2.pdf');
    ?>

Hey, great job!
It's good to be part of a success story :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.