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

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

    ---

    UPD
    Everything turned out to be simpler. I already have the Imagick extension installed on my PHP.
    I create a file via ISP, for example convert.php with 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 to images, which are available in the appropriate directory. Next, 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.
    JavaScript Chloe Hubbard, Jul 15, 2020

  • 3 Answers
  • 0
    $ imagick = new Imagick ();

    $ imagick- & gt; setResolution (300, 300);

    $ imagick- & gt; readImage ('test / myfile.pdf');

    $ imagick- & gt; writeImages ('test / myfile.png', false);
    Henry Kramer

  • 0
    Have you tried Google?) They say it sometimes helps ...

    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 Keep You!
    Anonymous

  • 0
    The first thing the search gives out, in my opinion, there you can find everything you need and even more.

    https://wpincode.com/9-luchshix-pdf-plaginov-dlya- ...
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!