/** * Bulk PDF import * * ## OPTIONS * * <source-dir> * : required, source directory to import PDF files * * <jpeg-compression-quality> * : optional, jpeg compression quality, default 60 * * <jpeg-resolution> * : optional, jpeg resolution, default 300 * * <post-status> * : optional, PDF post status, default "draft" * * <import-pdf-file> * : flag, if set then import PDF file to Wordpress media library * * ## EXAMPLES * * wp pdf-light-viewer bulk-import --source-dir="/path/to/pdfs" * wp pdf-light-viewer bulk-import --source-dir="/path/to/pdfs" --jpeg-compression-quality=60 --jpeg-resolution=300 --post-status=publish --import-pdf-file * * @synopsis --source-dir=<source-dir> [--jpeg-compression-quality=<jpeg-compression-quality>] [--jpeg-resolution=<jpeg-resolution>] [--post-status=<post-status>] [--import-pdf-file] * @subcommand bulk-import */ public function bulk_import($args, $assoc_args) { // options $source_dir = $assoc_args['source-dir']; $jpeg_compression_quality = isset($assoc_args['jpeg-compression-quality']) ? (int) $assoc_args['jpeg-compression-quality'] : 60; $jpeg_resolution = isset($assoc_args['jpeg-resolution']) ? (int) $assoc_args['jpeg-resolution'] : 300; $post_status = isset($assoc_args['post-status']) ? $assoc_args['post-status'] : 'draft'; $import_pdf_file = isset($assoc_args['import-pdf-file']); // check requirements $plugin_title = PdfLightViewer_Plugin::getData('Title'); $requirements_met = PdfLightViewer_Plugin::requirements(true); if (!$requirements_met) { $message = $plugin_title . ': ' . __('requirements not met, please check plugin settings page for more information.', PDF_LIGHT_VIEWER_PLUGIN); WP_ClI::error($message, true); } else { WP_CLI::log($plugin_title . ': ' . __("requirements are met, happy using!", PDF_LIGHT_VIEWER_PLUGIN)); } // check dir if (!is_readable($source_dir) || !is_dir($source_dir)) { WP_CLI::error(__("Source dir doesn't exist or it's not readable", PDF_LIGHT_VIEWER_PLUGIN), true); } else { WP_CLI::log(sprintf(__("Searching PDF files in %s", PDF_LIGHT_VIEWER_PLUGIN), $source_dir)); } // check PDF files $pdf_files = glob($source_dir . '/*.pdf', GLOB_NOSORT); if (empty($pdf_files)) { WP_CLI::error(__("Source dir doesn't contain PDF files", PDF_LIGHT_VIEWER_PLUGIN), true); } else { WP_CLI::log(sprintf(__("%d PDF files found", PDF_LIGHT_VIEWER_PLUGIN), count($pdf_files))); } // start import $pdf_files_count = count($pdf_files); $all_pdfs_progress = new \cli\progress\Bar(__("Processing PDF files", PDF_LIGHT_VIEWER_PLUGIN), $pdf_files_count); foreach ($pdf_files as $pdf_file_path) { // get number of pages $im = new Imagick(); $im->readImage($pdf_file_path); $pdf_pages_number = $im->getNumberImages(); foreach ($im as $_img) { $geometry = $_img->getImageGeometry(); $width = $geometry['width']; $height = $geometry['height']; break; } $im->destroy(); unset($im); $current_pdf_progress = new \cli\progress\Bar(sprintf(__("Processing PDF file %s", PDF_LIGHT_VIEWER_PLUGIN), $pdf_file_path), $pdf_pages_number); // create PDF post $post_id = wp_insert_post(['post_type' => PdfLightViewer_PdfController::$type, 'post_status' => $post_status, 'post_name' => sanitize_title(pathinfo($pdf_file_path, PATHINFO_FILENAME)), 'post_title' => pathinfo($pdf_file_path, PATHINFO_FILENAME)]); if (is_wp_error($post_id)) { WP_CLI::error(sprintf(__("Could not create PDF post: %s", PDF_LIGHT_VIEWER_PLUGIN), $post_id->get_error_message()), false); } else { // save pdf to media library if ($import_pdf_file) { $image_data = file_get_contents($pdf_file_path); $attach_id = PdfLightViewer_Plugin::create_media_from_data(pathinfo($pdf_file_path, PATHINFO_BASENAME), $image_data); update_post_meta($post_id, 'pdf_file_id', $attach_id); } $pdf_upload_dir = PdfLightViewer_Plugin::createUploadDirectory($post_id); $current_page = 1; $ratio = $width / $height; do_action(PDF_LIGHT_VIEWER_PLUGIN . ':before_import', $post_id, $pdf_file_path); update_post_meta($post_id, '_pdf-light-viewer-import-status', PdfLightViewer_PdfController::STATUS_CLI_PROCESSING); update_post_meta($post_id, '_pdf-light-viewer-import-progress', 0); update_post_meta($post_id, '_pdf-light-viewer-import-current-page', $current_page); update_post_meta($post_id, 'pdf-pages-number', $pdf_pages_number); update_post_meta($post_id, 'pdf-page-width', $width); update_post_meta($post_id, 'pdf-page-height', $height); // process pages for ($current_page; $current_page <= $pdf_pages_number; $current_page++) { $page_number = sprintf('%1$05d', $current_page); if (!file_exists($pdf_upload_dir . '/page-' . $page_number . '.jpg')) { try { PdfLightViewer_PdfController::process_pdf_page($post_id, $current_page, $page_number, $pdf_pages_number, $pdf_file_path, $pdf_upload_dir, $jpeg_resolution, $jpeg_compression_quality, $ratio); } catch (Exception $e) { PdfLightViewer_Plugin::log('Import exception: ' . $e->getMessage(), print_r($e, true)); $error = $e->getMessage(); update_post_meta($post_id, '_pdf-light-viewer-import-status', PdfLightViewer_PdfController::STATUS_FAILED); WP_CLI::warning(sprintf(__('Import of PDF %s failed: %s', PDF_LIGHT_VIEWER_PLUGIN), $pdf_file_path, $error), false); } } $current_pdf_progress->tick(); } do_action(PDF_LIGHT_VIEWER_PLUGIN . ':after_import', $post_id, $pdf_file_path); do_action(PDF_LIGHT_VIEWER_PLUGIN . ':finished_import', $post_id, $pdf_file_path); update_post_meta($post_id, '_pdf-light-viewer-import-status', PdfLightViewer_PdfController::STATUS_FINISHED); WP_CLI::success(sprintf(__('Import of PDF %s finished', PDF_LIGHT_VIEWER_PLUGIN), $pdf_file_path)); } $all_pdfs_progress->tick(); } WP_CLI::success(__('Import finished', PDF_LIGHT_VIEWER_PLUGIN)); }
public static function pdf_partially_import() { $unimported = PdfLightViewer_Model::getOneUnimported(); $post_id = $unimported->ID; if (!$post_id) { header('Content-Type: application/json'); echo json_encode(array('status' => 'error', 'progress' => 0, 'error' => __('Currently there are no unimported files in the queue.', PDF_LIGHT_VIEWER_PLUGIN))); return; } $status = PdfLightViewer_Plugin::get_post_meta($post_id, '_pdf-light-viewer-import-status', true); if ($status == static::STATUS_SCHEDULED) { $status_label = __('scheduled', PDF_LIGHT_VIEWER_PLUGIN); update_post_meta($post_id, '_pdf-light-viewer-import-status', static::STATUS_STARTED); } else { if ($status == static::STATUS_STARTED) { $status_label = __('started', PDF_LIGHT_VIEWER_PLUGIN); update_post_meta($post_id, '_pdf-light-viewer-import-status', 'processing'); } else { $status_label = __('processing', PDF_LIGHT_VIEWER_PLUGIN); } } ignore_user_abort(true); set_time_limit(0); $pdf_file_id = PdfLightViewer_Model::getPDFFileId($post_id); $pdf_file_path = get_attached_file($pdf_file_id); do_action(PDF_LIGHT_VIEWER_PLUGIN . ':before_import', $post_id, $pdf_file_path); $pdf_upload_dir = PdfLightViewer_Plugin::createUploadDirectory($post_id); $jpeg_resolution = PdfLightViewer_Plugin::get_post_meta($post_id, 'jpeg_resolution', true); $jpeg_compression_quality = PdfLightViewer_Plugin::get_post_meta($post_id, 'jpeg_compression_quality', true); $pdf_pages_number = (int) PdfLightViewer_Plugin::get_post_meta($post_id, 'pdf-pages-number', true); $current_page = (int) PdfLightViewer_Plugin::get_post_meta($post_id, '_pdf-light-viewer-import-current-page', true); $width = (int) PdfLightViewer_Plugin::get_post_meta($post_id, 'pdf-page-width', true); $height = (int) PdfLightViewer_Plugin::get_post_meta($post_id, 'pdf-page-height', true); if (!$width || !$height) { header('Content-Type: application/json'); echo json_encode(array('status' => 'error', 'progress' => 0, 'error' => __('Cannot get width and height of the first page.', PDF_LIGHT_VIEWER_PLUGIN))); return; } $ratio = $width / $height; if (!$current_page) { header('Content-Type: application/json'); echo json_encode(array('status' => 'error', 'progress' => 0, 'error' => __('Cannot detect current imported PDF page.', PDF_LIGHT_VIEWER_PLUGIN))); return; } $error = ''; for ($current_page; $current_page <= $pdf_pages_number; $current_page++) { $page_number = sprintf('%1$05d', $current_page); if (!file_exists($pdf_upload_dir . '/page-' . $page_number . '.jpg')) { try { $percent = static::process_pdf_page($post_id, $current_page, $page_number, $pdf_pages_number, $pdf_file_path, $pdf_upload_dir, $jpeg_resolution, $jpeg_compression_quality, $ratio); } catch (Exception $e) { PdfLightViewer_Plugin::log('Import exception: ' . $e->getMessage(), print_r($e, true)); $status_label = __('failed', PDF_LIGHT_VIEWER_PLUGIN); $error = $e->getMessage(); update_post_meta($post_id, '_pdf-light-viewer-import-status', static::STATUS_FAILED); } break; } } do_action(PDF_LIGHT_VIEWER_PLUGIN . ':after_import', $post_id, $pdf_file_path); if ($percent >= 100) { do_action(PDF_LIGHT_VIEWER_PLUGIN . ':finished_import', $post_id, $pdf_file_path); $status_label = __('finished', PDF_LIGHT_VIEWER_PLUGIN); update_post_meta($post_id, '_pdf-light-viewer-import-status', static::STATUS_FINISHED); } header('Content-Type: application/json'); echo json_encode(array('status' => $status_label, 'progress' => (int) $percent, 'error' => $error)); exit; }