使用 pdftoppm 将 PDF 档转为 JPG /JPEG/PNG/TIFF图片

多个图片合并到pdf

pdftoppm 是 Linux 系统上专门用来将 PDF 文件转为图片的工具,转档速度比 ImageMagick 更快,效果也很相当不错。其基本使用方式为:
# 使用 pdftoppm 将 PDF 档转为 JPG 图片档
pdftoppm -r 300 -jpeg input.pdf output
其中 -r 参数可用来指定每英寸点数(dpi),而 -jpeg 是指定输出图片格式为 JPG,这裡的输出档名不需要写副档名,pdftoppm 会以使用者指定的输出档名,再加上自动的编号与副档名来产生每一页的图片名称,把 PDF 的每一页各储存成一张图片。转出来的结果会像这样:
pdftoppm 将 PDF 转 JPG 图片
输出图片格式
pdftoppm 这个工具在转换 PDF 文件时,若不指定输出文件格式的话,它会将 PDF 的页面转为 PPM 这种图片格式:
# 预设会转换为 PPM 图片档
pdftoppm -r 300 input.pdf output
但 PPM 这种图片格式比较不常用,通常我们都会加上一些参数,输出比较常用的图片格式,除了 JPG 之外,它还支援 PNG 与 TIFF 等格式:
# 转换为 PNG 图片档
pdftoppm -png -r 300 input.pdf output
# 转换为 TIFF 图片档
pdftoppm -tiff -r 300 input.pdf output
选择部分页面
pdftoppm 支援好几种页面选择方式。-f 参数与 -l 参数可以分别用来指定开始的页码与结束的页码(页码从 1 开始),例如:
# 只抽取 input.pdf 的第二页到第五页,转为 JPG 档
pdftoppm -jpeg -r 300 -f 2 -l 5 input.pdf output
也可以使用 -o 与 -e 参数分别指定奇数页与偶数页:
# 只抽取 input.pdf 的奇数页,转为 JPG 档
pdftoppm -jpeg -r 300 -o input.pdf output
# 只抽取 input.pdf 的偶数页,转为 JPG 档
pdftoppm -jpeg -r 300 -e input.pdf output
若只需要转换 PDF 档的单一页,可以加上 -singlefile 参数,这样的话输出档名就不会加上任何编号:
# 只转换第一页,输出档名不加编号
pdftoppm -jpeg -r 300 -singlefile input.pdf output
# 只转换第三页,输出档名不加编号
pdftoppm -jpeg -r 300 -f 3 -singlefile input.pdf output
指定解析度
若要直接指定输出图片的解析度,可以使用 -scale-to 参数,它可以让使用者指定输出图片的长边长度,而比较短的那一边的长度,则会依照比例自动计算:
# 让输出图片的长边长度为 640 像素
pdftoppm -jpeg -scale-to 640 input.pdf output
若要直接指定宽度或高度,可以用 -scale-to-x 或 -scale-to-y 参数,通常建议的作法是只指定宽度或高度,另一个数值设定为 -1,让程式自动依比例计算,这样输出的图片才不会变形:
# 让输出图片的宽度为 640 像素,高度依比例调整
pdftoppm -jpeg -scale-to-x 640 -scale-to-y -1 input.pdf output
# 让输出图片的高度为 640 像素,宽度依比例调整
pdftoppm -jpeg -scale-to-x -1 -scale-to-y 640 input.pdf output
裁切区域
若要裁切输出的图形,只留下部份的区域,可以使用 -W 与 -H 指定区域的大小,并以 -x 与 -y 指定区域的位置,例如:
# 裁切一个宽度为 640 像素、高度为 360 像素的区域,
# 此区域距离左边界 60 像素、距离上边界 80 像素
pdftoppm -jpeg -x 60 -y 80 -W 640 -H 360 input.pdf output

如何转换 PDF 文件为Image (PNG, JPEG)图形化用GIMP命令行用pdftoppm工具

劳整理+详细记录:十部高分印度悬疑电影推荐,反转+烧脑

文本是译文。
原文:https://www.linuxuprising.com/2019/03/how-to-convert-pdf-to-image-png-jpeg.html

例子:按比例转换为宽度1024像素的图片:
pdftoppm -jpeg -scale-to 1024 $file filename

使用GIMP将PDF转换为图像(PNG、JPEG等)

GIMP是一个针对Linux、Windows和macOS的免费开源图像编辑器,可以将PDF文档的页面导出为各种图像格式,包括PDF、JPEG、TIFF、BMP等。

使用GIMP打开PDF文档时,每个页面都作为单独的层添加,一次只能将一个PDF页面导出为图像。您可以通过使用GIMP插件一次性将所有PDF页面(GIMP层)或选定的一组层导出为单独的图像。

使用GIMP将一个或几个PDF页面转换为PNG、JPEG和其他图像格式。

让我们从使用GIMP将单个(或仅几个)PDF页面转换为PNG、JPEG等开始,这不需要安装任何插件。从GIMP文件菜单中单击打开并选择PDF文件。将弹出“从PDF导入”对话框-确保“打开页面为”选项设置为“层”,然后单击“导入”:

原英文:
This article explains how to convert the pages of a PDF document to image files (PNG, JPEG, and others). For this task we'll be using (you can choose the one you prefer):
GIMP (GNU Image Manipulation Program): for those wanting to use use a graphical application for this task. Since by default GIMP can't export all PDF pages automatically (it requires exporting pages one by one), the article also includes a GIMP plugin that can export all layers as separate images.
pdftoppm: this command line tool is part of the poppler / poppler-utils package, and it can convert PDF documents to images (with each PDF page as a separate image) like PNG, JPEG and others. The tool can convert a single page of a PDF document, all the pages, or a page range, and it comes with multiple options like specifying the resolution, image cropping, and more.
Convert PDF to image (PNG, JPEG, etc.) using GIMP
GIMP, a free and open source image editor for Linux, Windows and macOS, can export pages of PDF documents to various image formats, including PDF, JPEG, TIFF, BMP, and many others.
When opening a PDF document with GIMP, each page is added as a separate layer, and only one PDF page at a time can be exported as an image. You can export all the PDF pages (GIMP layers) or a selected group of layers as separate images in one go by using a GIMP plugin.
Convert one or only a few PDF pages to PNG, JPEG and other image formats using GIMP.
Let's start with converting a single (or only a few) PDF page as PNG, JPEG, etc., using GIMP, which doesn't require installing any plugins. From the GIMP File menu click Open and choose the PDF file. An Import from PDF dialog should pop up - make sure the Open pages as option is set to Layers, and click Import:
GIMP Import from PDF dialog
In the GIMP layers dialog, scroll to the page you want to convert from PDF to PNG, JPEG and so on, and drag it with the mouse cursor to the top so it's the first layer:
GIMP Layers
Next, from the GIMP File menu click Export As.... In the export dialog you can either change the exported filename extension to the desired image format (in the Name field at the top), or you can click Select File Type (By Extension) at the bottom of the dialog to choose the format that will be used for the exported PDF page:
Now all you have to do is press the Export button to save the PDF page as a PNG, JPEG, TIFF or some other image format.
Depending on the image type you'll use for your exported PDF page, GIMP will show a dialog that lets you change some settings, like the image quality, compression level, and so on. Make any changes you want here, then click Export and the image file will be saved to your computer.
If you want to export other pages of the PDF document to an image, move the layer containing the page to the top of the layer list in GIMP, and repeat the process. For converting an entire PDF (or a large number of PDF pages in one go) to JPEG, PNG, and other image formats using GIMP, see the instructions below.
Using the GIMP Export Layers plugin to export all/select layers (PDF pages in this case) as separate images.
Export Layers is a GIMP plugin that can export layers as separate images. It can export layers in any file format supported by GIMP, and it comes with various options, like exporting all layers, only visible layers, exporting only selected layers, and more.
Download Export Layers plugin for GIMP.
You can easily install this plugin on Windows by using provided Windows installer. To install it on Linux or macOS, download the Export Layers ZIP archive, extract it, and copy the export_layers.py file and the export_layers folder to:
Linux:
GIMP 2.8: ~/.gimp-2.8/plug-ins
GIMP 2.10 installed using your Linux distribution's package manager: ~/.config/GIMP/2.10/plug-ins
GIMP 2.10 installed from the Flathub Flatpak package: ~/.var/app/org.gimp.GIMP/config/GIMP/2.10/plug-ins
GIMP 2.10 installed using snap: ~/snap/gimp/current/.config/GIMP/2.10/plugins
macOS:
GIMP 2.8: /Users/[your username]/Library/Application Support/GIMP/2.8/plug-ins
GIMP 2.10: /Users/[your username]/Library/Application Support/GIMP/2.10/plug-ins
Restart GIMP if it was running when you installed the plugin.
More about installing the Export Layers GIMP plugin can be found in its documentation.
Now open a PDF file with GIMP and make sure the Open pages as option is set to Layers in the import dialog, as already explained above.
You can now convert all the PDF pages or a select group of pages to image formats like PNG, JPEG, and any other format supported by GIMP, by clicking File -> Export Layers... from the GIMP menu. In case you want to export all the PDF pages as separate PNG images, all you have to do is choose the save location and press the Export button.
The Export Layers dialog lets you change the file extension, as well as perform other advanced operations. Click the png field at the bottom of the dialog to change the image format:
GIMP export all layers as PNG images
For advanced options, click Settings at the bottom of the Export Layers dialog, and then Show more settings.
For example, if you want to only export the layers you select in the preview dialog (shown on the right-hand side of the Export Layers dialog), click + Add Constraint..., and then Only layers selected in the preview:
GIMP export selected PDF layers to PNG
You can now click a layer in the right-hand side preview sidebar, then hold the Shift key and click another layer to select all layers in between. Or hold the Ctrl key and click on each layer you want to export.
All that remains to be done now is to click the Export button, and GIMP will start exporting the PDF pages you've selected (or all) to PNG, JPEG or whatever image format you chose:
GIMP Exporting layers as PNG images
For more about customizing the export using Export Layers, visit the plugin documentation page.
You might also be interested in: Configure GIMP 2.10 To Use Photoshop Keyboard Shortcuts (How-To)
Convert PDF to images (PNG, JPEG, more) from the command line using pdftoppm

以下是命令行安装和使用:
pdftoppm can convert PDF document pages to image formats like PNG, JPEG, and others, from the command line. In can convert all the pages of a PDF document to separate PDF files, a single page or a page range, it supports specifying the image resolution, scale, crop the resulting images, and much more.
pdftoppm is part of the poppler / poppler-utils / poppler-tools package (depending on the Linux distribution you're using). Install this package as follows:
Debian, Ubuntu, Linux Mint, and other Debian/Ubuntu-based Linux distributions:
sudo apt install poppler-utils
Fedora:
sudo dnf install poppler-utils
openSUSE:
sudo zypper install poppler-tools
Arch Linux:
sudo pacman -S poppler
In other Linux distributions use your package manager to install the poppler / poppler-utils package.
Now let's start by using pdftoppm to convert an entire PDF (we'll call it document.pdf in this example) to PNG, and name each page of the PDF converted to PNG as document-01.png, document-02.png and so on:
pdftoppm -png document.pdf document
Want to convert the PDF pages to JPEG instead of PNG? Use -jpeg instead. For TIFF, use -tiff. If you don't specify an image format, pdftoppm will convert the PDF pages to PPM image files by default.
To define a page range for the PDF to image conversion with pdftoppm, use -f N, where N in this case is the page number of the first page to print, and -l N, where N in this case is the last page to print.
For example to convert pages 5 to 15 from a PDF document (named document.pdf in the example) to PNG with pdftoppm, and have each resulting image named document-05.png, document-06.png and so on, you'd use:
pdftoppm -png -f 5 -l 15 document.pdf document
Want to increase the converted image DPI? By default pdftoppm converts PDF pages to images with a DPI of 150. To increase the DPI, add -rx and -ry to the command, with your custom values.
For example, to use a DPI of 300 for the resulted images, use:
pdftoppm -png -rx 300 -ry 300 document.pdf document
To see all the options supported by pdftoppm, run pdftoppm --help and man pdftoppm.

 

ubuntu下工具pdftoppm代替pdfimages转换pdf为jpeg或者png

多个图片合并到pdf

假设有批量pdf文件,需要转为jpeg文件,按pdf文件名称建立目录,把图片放到目录下,以阿拉伯数字命名。

#!/bin/bash
find . -type f -name \*.pdf |awk -F "/" '{print $2}' > allpdf.lst
for file in `cat allpdf.lst`
do
mkdir ${file%%.pdf};
echo "pdf name is: " $file
echo "mkdir is: " ${file%%.pdf}
pdftoppm -jpeg $file goodluck
#pdfimages -j $file ${file%%.pdf}

rename 's/goodluck-00//' *.jpg
rename 's/goodluck-0//' *.jpg
rename 's/goodluck-//' *.jpg

mv *.jpg ${file%%.pdf}/
done;

# pdftoppm -h
pdftoppm version 0.62.0
Copyright 2005-2017 The Poppler Developers - http://poppler.freedesktop.org
Copyright 1996-2011 Glyph & Cog, LLC
Usage: pdftoppm [options] [PDF-file [PPM-file-prefix]]
-f <int> : first page to print
-l <int> : last page to print
-o : print only odd pages
-e : print only even pages
-singlefile : write only the first page and do not add digits
-r <fp> : resolution, in DPI (default is 150)
-rx <fp> : X resolution, in DPI (default is 150)
-ry <fp> : Y resolution, in DPI (default is 150)
-scale-to <int> : scales each page to fit within scale-to*scale-to pixel box
-scale-to-x <int> : scales each page horizontally to fit in scale-to-x pixels
-scale-to-y <int> : scales each page vertically to fit in scale-to-y pixels
-x <int> : x-coordinate of the crop area top left corner
-y <int> : y-coordinate of the crop area top left corner
-W <int> : width of crop area in pixels (default is 0)
-H <int> : height of crop area in pixels (default is 0)
-sz <int> : size of crop square in pixels (sets W and H)
-cropbox : use the crop box rather than media box
-mono : generate a monochrome PBM file
-gray : generate a grayscale PGM file
-png : generate a PNG file
-jpeg : generate a JPEG file
-jpegopt <string> : jpeg options, with format <opt1>=<val1>[,<optN>=<valN>]*
-tiff : generate a TIFF file
-tiffcompression <string>: set TIFF compression: none, packbits, jpeg, lzw, deflate
-freetype <string> : enable FreeType font rasterizer: yes, no
-thinlinemode <string> : set thin line mode: none, solid, shape. Default: none
-aa <string> : enable font anti-aliasing: yes, no
-aaVector <string> : enable vector anti-aliasing: yes, no
-opw <string> : owner password (for encrypted files)
-upw <string> : user password (for encrypted files)
-q : don't print any messages or errors
-v : print copyright and version info
-h : print usage information
-help : print usage information
--help : print usage information
-? : print usage information