T
T
turbomen242020-10-12 05:57:23
Python
turbomen24, 2020-10-12 05:57:23

Is it possible, when inserting a pdf into a site, to automatically display its pages as images?

I'm using Wordpress as CSM and the challenge is this: when I insert a pdf through the page editor, display it as a set of images (in any format like jpg).
Is there any way to do this automatically? That is, I inserted a pdf , and as a result, images are displayed on the site, one for each page from the PDF.
Curious, what are the means to solve this problem? Python?
At worst, maybe there is a plugin for WP that can do this?

---

UPD
Everything turned out to be easier. I already have the Imagick extension installed on my PHP.
I create a file via ISP, for example convert.php with the content:

$imagick = new Imagick();
$imagick->setResolution(300, 300);
$imagick->readImage('test/myfile.pdf');
$imagick->writeImages('test/myfile.png', false);


This code converts all PDF pages into images that are available in the appropriate directory. Then you can create functions so that when you upload a pdf through a form on the site, pictures are created and inserted into the page.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
T
turbomen24, 2020-10-12
@turbomen24

$imagick = new Imagick();
$imagick->setResolution(300, 300);
$imagick->readImage('test/myfile.pdf');
$imagick->writeImages('test/myfile.png', false);

O
Orkhan Hasanli, 2020-10-12
@azerphoenix

Have you tried Google?) They say it helps sometimes...
https://stackoverflow.com/questions/31534746/creat...

function process_pdf( $file ) {

    if( $file['type'] === 'application/pdf' ) {

        // Get the parent post ID, if there is one
        if( isset($_REQUEST['post_id']) ) {
            $post_id = $_REQUEST['post_id'];

            $filename = $file[ 'name' ];
            $filename_wo_extension = basename( $filename, ".pdf" );

            $im = new Imagick();
            $im->setResolution(300, 300);
            $im->readimage( $file[ 'tmp_name' ] ); 
            $pages = $im->getNumberImages();

            $attachments_array = array();

            // iterate over pages of the pdf file
            for($p = 1; $p <= $pages; $p++){
                $im->setIteratorIndex( $p - 1 );
                $im->setImageFormat('jpeg');

                $filename_neu = $filename_wo_extension .'_'. $p .'.jpg';

                // upload new image to wordpress
                // https://codex.wordpress.org/Function_Reference/wp_insert_attachment
                $upload_file = wp_upload_bits($filename_neu, null, $im);
                if (!$upload_file['error']) {

                    $attachment = array(
                        'post_mime_type' => 'image/jpeg',
                        'post_title' => preg_replace( '/\.[^.]+$/', '', $filename_neu),
                        'post_content' => '',
                        'post_status' => 'inherit'
                    );

                    $attachment_id = wp_insert_attachment( $attachment, $upload_file['file'] );

                    if (!is_wp_error( $attachment_id )) {
                        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
                        $attachment_data = wp_generate_attachment_metadata( $attachment_id, $upload_file['file'] );
                        wp_update_attachment_metadata( $attachment_id,  $attachment_data );
                        $attachments_array[] = $attachment_id;
                    }
                }
            }

            // add new images to a gallery (advanced custom fields plugin)
            // http://www.advancedcustomfields.com/resources/update_field/
            update_field( 'field_55b0a473da995', $attachments_array, $post_id );

          $im->destroy();
       }
    }

    return $file;

}

add_filter('wp_handle_upload_prefilter', 'process_pdf' );

https://wordpress.org/plugins/pdf-image-generator/
Google bless you!

P
Pavstyuk, 2020-10-12
@Pavstyuk

The first thing that comes up in a search is that in my opinion you can find everything you need and even more.
https://wpincode.com/9-luchshix-pdf-plugin-dlya-...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question