我正在尝试使用命令行程序转换将PDF转换为图像(JPEG或PNG)。这是我正在转换的pdf文件之一。

我想让程序去掉多余的空白,并返回足够高质量的图像,以便上标可以轻松读取。

这是我目前最好的尝试。正如你所看到的,修剪工作很好,我只是需要锐化的分辨率相当多。这是我正在使用的命令:

convert -trim 24.pdf -resize 500% -quality 100 -sharpen 0x1.0 24-11.jpg

我试着做了以下有意识的决定:

调整它的大小(对分辨率没有影响) 尽可能提高质量 使用-锐化(我已经尝试了一系列值)

任何建议,请在最终的PNG/JPEG图像的分辨率更高,将非常感谢!


当前回答

我发现,当使用转换使用的底层gs(又名Ghostscript)命令将大型pdf批量处理为png和jpg时,它既更快又更稳定。

你可以在convert -verbose的输出中看到这个命令,还有一些可能的调整(YMMV),这些调整很难/不可能直接通过convert访问。

然而,这将很难做你的修剪和锐化使用gs,所以,正如我所说的,YMMV!

其他回答

另一个建议是您可以使用GIMP。

只需在GIMP->中加载PDF文件,另存为.xcf,然后你就可以对图像做任何你想做的事情了。

我使用开源java pdf引擎icepdf。检查办公室演示。

package image2pdf;

import org.icepdf.core.exceptions.PDFException;
import org.icepdf.core.exceptions.PDFSecurityException;
import org.icepdf.core.pobjects.Document;
import org.icepdf.core.pobjects.Page;
import org.icepdf.core.util.GraphicsRenderingHints;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class pdf2image {

   public static void main(String[] args) {

      Document document = new Document();
      try {
         document.setFile("C:\\Users\\Dell\\Desktop\\test.pdf");
      } catch (PDFException ex) {
         System.out.println("Error parsing PDF document " + ex);
      } catch (PDFSecurityException ex) {
         System.out.println("Error encryption not supported " + ex);
      } catch (FileNotFoundException ex) {
         System.out.println("Error file not found " + ex);
      } catch (IOException ex) {
         System.out.println("Error IOException " + ex);
      }

      // save page captures to file.
      float scale = 1.0f;
      float rotation = 0f;

      // Paint each pages content to an image and
      // write the image to file
      for (int i = 0; i < document.getNumberOfPages(); i++) {
         try {
         BufferedImage image = (BufferedImage) document.getPageImage(
             i, GraphicsRenderingHints.PRINT, Page.BOUNDARY_CROPBOX, rotation, scale);

         RenderedImage rendImage = image;
         try {
            System.out.println(" capturing page " + i);
            File file = new File("C:\\Users\\Dell\\Desktop\\test_imageCapture1_" + i + ".png");
            ImageIO.write(rendImage, "png", file);
         } catch (IOException e) {
            e.printStackTrace();
         }
         image.flush();
         }catch(Exception e){
             e.printStackTrace();
         }
      }

      // clean up resources
      document.dispose();
   }
}

我也尝试过imagemagick和pdftoppm, pdftoppm和icepdf的分辨率都比imagemagick高。

看来以下方法是有效的:

convert           \
   -verbose       \
   -density 150   \
   -trim          \
    test.pdf      \
   -quality 100   \
   -flatten       \
   -sharpen 0x1.0 \
    24-18.jpg

它的结果是左图。将其与我的原始命令的结果进行比较(右侧的图像):

  

(为了真正看到和欣赏两者之间的差异,右键单击每个,并选择“在新选项卡中打开图像…”)

还要记住以下事实:

右边最糟糕的模糊图像的文件大小为1.941.702字节(1.85 MByte)。 它的分辨率是3060x3960像素,使用16位RGB颜色空间。 左边图像比较清晰,文件大小为337.879字节(330 kByte)。 它的分辨率是758x996像素,使用8位灰色空间。

所以,不需要调整大小;添加-density标志。密度值150很奇怪——尝试一个范围的值会导致图像在两个方向上看起来都很糟糕!

在投票之前请注意,这个解决方案是针对使用图形界面的Gimp的,而不是使用命令行的ImageMagick的,但它作为一个替代方案对我来说效果非常好,这就是为什么我发现有必要在这里分享它。

按照这些简单的步骤从PDF文档中提取任何格式的图像

Download GIMP Image Manipulation Program Open the Program after installation Open the PDF document that you want to extract Images Select only the pages of the PDF document that you would want to extract images from. N/B: If you need only the cover images, select only the first page. Click open after selecting the pages that you want to extract images from Click on File menu when GIMP when the pages open Select Export as in the File menu Select your preferred file type by extension (say png) below the dialog box that pops up. Click on Export to export your image to your desired location. You can then check your file explorer for the exported image.

这是所有。

我希望这对你们有帮助

这适用于从多个PDF和图像文件创建单个文件:

php exec('convert -density 300 -trim "/path/to/input_filename_1.png" "/path/to/input_filename_2.pdf" "/path/to/input_filename_3.png" -quality 100 "/path/to/output_filename_0.pdf"');

地点:

-density 300 = dpi

-trim =一些关于透明度的东西-使边缘看起来光滑

质量100 =质量vs压缩(100%质量)

平……对于多页,不要使用“压平”