I am using Advanced Custom Fields plugin(free version) for attaching 3 different pdf files to a single post.

After displaying a list of posts on a template, with a button click I am making an AJAX Request to get the files of all displayed posts.

Since every post has 3 PDF files, I want to merge all these files of all posts to a single PDF file and download that single file.

What I did so far :

AJAX Request on button click:

    function merklisteDownload() {
        window.event.preventDefault()
        var m = JSON.parse(window.sessionStorage.getItem('merkliste_items')) || []
        var ids = []
        if (m.length > 0) {
            m.forEach(element => {
                ids.push(element.id)
            });
        }
        $.ajax({
            type: 'POST',
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            dataType: "json",
            data: {
                action: 'get_post_files',
                ids: ids
            },
            success: function(response) {
                console.log(response.data)
            },
            error: function(error) {
                console.log(error)
            }
        });
    }

And admin-ajax.php code:

    function get_post_files(){
    $data = [];
    foreach ($_POST['ids'] as $post_id) {
        array_push($data, [
            'id'                        => $post_id,
            'title'                     => get_the_title($post_id),
            'category'                  => get_the_category($post_id)[0]->name,
            'front_image'               => get_field('front_image', $post_id)['sizes']['large'] == true ? get_field('front_image', $post_id)['sizes']['large'] : null,
            'back_image'                => get_field('back_image', $post_id)['sizes']['large'] == true ? get_field('back_image', $post_id)['sizes']['large'] : null,
            'technische_daten'          => get_field('technische_daten', $post_id)['url'], //file
            'lichttechnische_parameter' => get_field('lichttechnische_parameter', $post_id)['url'], //file
            'reach_zertifikat'          => get_field('reach-zertifikat', $post_id)['url'],  //file
        ]);
    }
    wp_send_json_success($data);
    wp_die();
}

add_action('wp_ajax_get_post_files', 'get_post_files');
add_action('wp_ajax_nopriv_get_post_files', 'get_post_files');

Everything is working till now, I am able to get the file URLs of each post, but I dont know how to merge these files into one and return the file as AJAX Response, then download that file.

Any help?

Best regards.

1

There are 1 best solutions below

2
On

Here is an option that may point you in the right direction to merge the files: https://orangeable.com/php/pdf-merge

It would probably look something like this:

function get_post_files(){
    $data = [];
    foreach ($_POST['ids'] as $post_id) {
        array_push($data, [
            'id'                        => $post_id,
            'title'                     => get_the_title($post_id),
            'category'                  => get_the_category($post_id)[0]->name,
            'front_image'               => get_field('front_image', $post_id)['sizes']['large'] == true ? get_field('front_image', $post_id)['sizes']['large'] : null,
            'back_image'                => get_field('back_image', $post_id)['sizes']['large'] == true ? get_field('back_image', $post_id)['sizes']['large'] : null,
            'technische_daten'          => get_field('technische_daten', $post_id)['url'], //file
            'lichttechnische_parameter' => get_field('lichttechnische_parameter', $post_id)['url'], //file
            'reach_zertifikat'          => get_field('reach-zertifikat', $post_id)['url'],  //file
        ]);
        $temp = array(
            $data['technische_daten'],
            $data['lichttechnische_parameter'],
            $data['reach_zertifikat']
        );
        file_put_contents(getcwd() . "\\pdf\\merged-".$post_id.".pdf", "");
        $command = "gs -q -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=" . getcwd() . "\\pdf\\merged-".$post_id.".pdf "; // needs space at end
        foreach($temp as $file) {
            $command .= getcwd() . "\\pdf\\" . $file . " ";
        }
        $command .= "2&>1";
        $data['merged_file'] = shell_exec($command);
    }
    wp_send_json_success($data);
    wp_die();
}

add_action('wp_ajax_get_post_files', 'get_post_files');
add_action('wp_ajax_nopriv_get_post_files', 'get_post_files');