本文转自知乎 https://zhuanlan.zhihu.com/p/197919733

    在 TeX 的使用上,不可避免的是要使用到 PDF 格式文件:现在几乎所有的 TeX 都是直接支持 PDF 输出;插图也大多是 PDF 格式的。

    社区里,几乎所有能找到的工具大多是基于下面几个开源项目的:

    • Ghostscript,PDF 及 PS 的解释器
    • MuPDF,PDF 解释器 / 预览器
    • Poppler/Xpdf,PDF 解析 / 显示库

    本文介绍的一些小技巧跟这几个开源项目的工具有关。

    1. EPS 文件到 PDF 的转换

    就算到了 2020 年,我依然能够发现某些学生或者某些期刊还在使用或者要求使用 EPS 格式文件来插图。就我的了解而言,似乎没有哪一家的排版流程完全是基于 PostScript 的。所以 EPS 这东西没必要用,就算你用了 EPS 文件,最后还是要转换成 PDF 文件的。所以,我建议使用某些工具软件(matlab 之类)导出图像的,直接导出 PDF 就好。

    万一不幸,需要处理,其实也比较简单,直接用 ps2pdf 工具即可(TeX Live 中自带):

    1. Usage: ps2pdf [options] <inputfile> <outputfile>
    2. <inputfile> can be either a PS, EPS, or PDF file.
    3. A single hyphen (-) denotes stdin.
    4. <outputfile> is required if <inputfile> is a PDF file
    5. or input is read from stdin.

    2. PDF 文件裁边

    某些 PDF 文件需要裁边,具体场景比如:把 PDF 扔到 Kindle 里面去读;插图的时候避免多余的空白。这时候可以使用 pdfcrop 工具(TeX Live 中自带)。简单的用法就是敲个命令:

    但是这个工具的参数实在是太多了,在使用的时候,建议对照着 help 调整一下,多试几次:

    1. PDFCROP 1.40, 2020/06/06 - Copyright (c) 2002-2020 by Heiko Oberdiek, Oberdiek Package Support Group.
    2. Syntax: pdfcrop [options] <input[.pdf]> [output file]
    3. Function: Margins are calculated and removed for each page in the file.
    4. Options: (defaults:)
    5. --help print usage
    6. --version print version number
    7. --(no)verbose verbose printing (false)
    8. --(no)debug debug informations (false)
    9. --gscmd <name> call of ghostscript (gswin32c)
    10. --pdftex | --xetex | --luatex
    11. use pdfTeX | use XeTeX | use LuaTeX (pdftex)
    12. --pdftexcmd <name> call of pdfTeX (pdftex)
    13. --xetexcmd <name> call of XeTeX (xetex)
    14. --luatexcmd <name> call of LuaTeX (luatex)
    15. --margins "<left> <top> <right> <bottom>" (0 0 0 0)
    16. add extra margins, unit is bp. If only one number is
    17. given, then it is used for all margins, in the case
    18. of two numbers they are also used for right and bottom.
    19. --(no)clip clipping support, if margins are set (false)
    20. (not available for --xetex)
    21. --(no)hires using `%%HiResBoundingBox' (false)
    22. instead of `%%BoundingBox'
    23. --(no)ini use iniTeX variant of the TeX compiler (false)
    24. Expert options:
    25. --restricted turn on restricted mode (false)
    26. --papersize <foo> parameter for gs's -sPAPERSIZE=<foo>,
    27. use only with older gs versions <7.32 ()
    28. --resolution <xres>x<yres> ()
    29. --resolution <res> pass argument to ghostscript's option -r
    30. Example: --resolution 72
    31. --bbox "<left> <bottom> <right> <top>" ()
    32. override bounding box found by ghostscript
    33. with origin at the lower left corner
    34. --bbox-odd Same as --bbox, but for odd pages only ()
    35. --bbox-even Same as --bbox, but for even pages only ()
    36. --pdfversion <x.y> | auto | none
    37. Set the PDF version to x.y, x= 1 or 2, y=0-9.
    38. If `auto' is given as value, then the
    39. PDF version is taken from the header
    40. of the input PDF file.
    41. An empty value or `none' uses the
    42. default of the TeX engine. (auto)
    43. --uncompress creates an uncompressed pdf,
    44. useful for debugging (false)
    45. Input file: If the name is `-', then the standard input is used and
    46. the output file name must be explicitly given.
    47. Examples:
    48. pdfcrop --margins 10 input.pdf output.pdf
    49. pdfcrop --margins '5 10 5 20' --clip input.pdf output.pdf
    50. In case of errors:
    51. Try option --verbose first to get more information.
    52. In case of bugs:
    53. Please, use option --debug for bug reports.

    如果你使用的是 Mac 系统,有时候截图可能更省事一点,因为 Preview 自带截图工具,大致如下:

    工具:PDF处理的一些小技巧 - 知乎 - 图1

    3. PDF 文件转为图像(位图)

    这个我用的比较多,比如写公众号文章或者知乎文章的时候,就需要把某些输出的结果转换成图。(现在都偷懒到直接用 PC 版微信的截图功能了)

    转换成图像就有两个方法。

    MuPDF 工具(速度略快一点的):

    1. mutool draw -r 300 -o i.png i.pdf

    这里面 - r 是 dpi 大小(默认是 72,想要图片清晰,就增大数值),-o 是输出文件名,如果是多页,可以写成格式化字符串样式,比如:

    1. mutool draw -r 300 -o i-%d.png i.pdf

    Ghostscript(速度略慢一点的):

    1. gs -sDEVICE=pngalpha -o file-%03d.png -r300 i.pdf

    如果是在 Windows 下的 TeX Live 中,可以这样使用:

    1. rungs -sDEVICE=pngalpha -o file-%03d.png -r300 i.pdf

    4. PDF 文件转为图像(矢量)

    这里面,所谓的矢量图像,其实 PDF 就是一个,比如我们可以通过下面的命令实现其他软件中的 “转曲” 功能:

    1. gs -o o.pdf -dNoOutputFonts -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER i.pdf

    但有时候我们需要输出到 SVG 或者 EPS(现实如此),那么可以使用 poppler 的 pdftocairo 工具:

    1. pdftocairo -svg i.pdf
    2. pdftocairo -eps i.pdf

    MuPDF 的工具也支持:

    1. mutool convert -F svg i.pdf

    5. 查看 / 抽取文档内的字体和图像

    这个可能算一点逆向。有时候分析某些 PDF 的排版,就需要查看字体。而某些文章,比如想要取出原图,也是可以操作的。

    查看字体可以使用 poppler 的 pdffonts:

    取出 PDF 文件中的字体和图像可以使用:

    6. 抽取 PDF 页面

    偶尔需要抽出一定范围的页面或者单独的几个页面的时候(比如第 1 页,第 2 页,第 3 页,第 6-7 页),可以这样:

    1. mutool merge -o o.pdf i.pdf 1,2,3,6-7

    虽然我们看到了 merge 字眼,而且 MuPDF 的这个工具确实是也是做合并的。但我要说的是,抽取单个 PDF 中的页面确实是某种特殊的合并操作。merge 的全部操作如下:

    1. usage: mutool merge [-o output.pdf] [-O options] input.pdf [pages] [input2.pdf] [pages2] ...
    2. -o - name of PDF file to create
    3. -O - comma separated list of output options
    4. input.pdf name of input file from which to copy pages
    5. pages comma separated list of page numbers and ranges
    6. PDF output options:
    7. decompress: decompress all streams (except compress-fonts/images)
    8. compress: compress all streams
    9. compress-fonts: compress embedded fonts
    10. compress-images: compress images
    11. ascii: ASCII hex encode binary streams
    12. pretty: pretty-print objects with indentation
    13. linearize: optimize for web browsers
    14. clean: pretty-print graphics commands in content streams
    15. sanitize: sanitize graphics commands in content streams
    16. garbage: garbage collect unused objects
    17. incremental: write changes as incremental update
    18. continue-on-error: continue saving the document even if there is an error
    19. or garbage=compact: ... and compact cross reference table
    20. or garbage=deduplicate: ... and remove duplicate objects
    21. decrypt: write unencrypted document
    22. encrypt=rc4-40|rc4-128|aes-128|aes-256: write encrypted document
    23. permissions=NUMBER: document permissions to grant when encrypting
    24. user-password=PASSWORD: password required to read document
    25. owner-password=PASSWORD: password required to edit document

    小技巧就先介绍这些,有其他的问题的,欢迎留言。

    本文首发于公众号 “学术与 TeX”,欢迎搜索关注本公众号。
    https://zhuanlan.zhihu.com/p/197919733