Merge PDF files from post custom fields(ACF) into one PDF file and respond back the created PDF file, Wordpress(AJAX Request)

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.

Topic advanced-custom-fields ajax pdf Wordpress

Category Web

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.