找回密码
 快速注册
搜索
查看: 75|回复: 6

[Ghostscript]裁剪pdf页面

[复制链接]

3147

主题

8381

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65357
QQ

显示全部楼层

hbghlyj 发表于 2022-5-18 00:56 |阅读模式
本帖最后由 hbghlyj 于 2022-8-11 19:11 编辑 Linux下ghostscript的指令是gs, Windows下则是gswin32c.exe .(以下示例为linux)
首先用wget从Internet下载pdf文件到当前目录:
  1. wget http://pcjohnson.net/312/Sqrt.pdf
复制代码

提取第9页到outfile_9.pdf
  1. gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=9  -dLastPage=9 -sOutputFile=outfile_9.pdf Sqrt.pdf
复制代码
得到:
1.png
再进行裁剪.参数顺序:左框到原左框的距离121,下框到原下框的距离120,右框到原左框的距离523,上框到原下框的距离359. 单位都是postscript point,即1/72 inch.
  1. gs -o cropped.pdf -sDEVICE=pdfwrite -c "[/CropBox [121 120 523 359]" -c " /PAGES pdfmark" -f outfile_9.pdf
复制代码
1.png
参考:stackoverflow.com/questions/6183479/cropping-a-pdf-using-ghostscript-9-01
再将pdf转换为svg
  1. pdftocairo -svg cropped.pdf Sqrt.svg
复制代码

$type Sqrt.svg (38.23 KB, 下载次数: 88)

注1.
如果参数顺序错了,gs不会报错,但是会输出空的pdf(只有一条缝).如果使用pdfcrop,它就会报错
!!!Warning: Empty Bounding Box is returned by Ghostscript!
!!!   Page 1: 121 120 121 432
!!! Either there is a problem with the page or with Ghostscript.
!!! Recovery is tried by embedding the page in its original size.


注2.
另外,gs可以从pdf中分离image,vector,text三者.在这个例子中,“坐标轴”,“函数曲线”,“分数线”,“分隔线”都属于vector(矢量图).
参考:stackoverflow.com/questions/29657335/how-can-i-remove-all-images-from-a-pdf/37858893#37858893

关于“PDF”格式的资料:
Many PDF documents may contain vector artwork or line-art. However, due to the nature of PDF, it is not always possible for a computer program to determine where on a page such artwork occurs since each page is stored as a general mix of text, images and line-art.

3147

主题

8381

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65357
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2022-5-18 02:03


本楼将SVG附件转换为svg并替换附件
  1. async function startconvert(){const url = $('attach_11673').firstElementChild.href;
  2. const response = await fetch(url);
  3. const decoder = new TextDecoder();
  4. const reader = response.body.getReader();
  5. str = '';
  6. while (true) {
  7. const { value, done } = await reader.read();
  8. if (done) break;
  9. str += decoder.decode(value);
  10. }
  11. $('attach_11673').parentNode.outerHTML = str}
  12. startconvert()
复制代码

3147

主题

8381

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65357
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2022-5-18 02:17

[Javascript] async函数

developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/async_function
async函数可能包含0个或者多个await表达式。await表达式会暂停整个async函数的执行进程并出让其控制权,只有当其等待的基于promise的异步操作被兑现或被拒绝之后才会恢复进程。

3147

主题

8381

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65357
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2022-5-29 00:43
SumatraPDF中,按M可以显示光标位置, 再按M就会切换单位 (有mm,in,pt三种单位)
用in(英寸)的数值乘以72就得到bp的数值了
QQ图片20220401132641.png

在Inkscape中,如果另存为optimized svg,在几何图中,标记顶点用的黑色的“圆点”就不见了,但是另存为“plain svg”是正常的.

3147

主题

8381

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65357
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2022-8-12 02:07
2022.08.11更新:
在1楼, 用"pdftocairo -svg cropped.pdf Sqrt.svg"替换了"pdf2svg cropped.pdf Sqrt.svg"
因为pdfsvg的作者写道:
Note: since this utility was written, the maintainers of Poppler have written a utility that works on the same principle: pdftocairo. I recommend that you use their utility since it is better maintained than mine.
他推荐用pdftocairo, 因为它比pdf2svg的维护得更好。

3147

主题

8381

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65357
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-2-21 07:17
Crop PDF files with Ghostscript
Normally, with the following command and justifications on the CropBox parameters, it should work.
  1. gs -o cropped.pdf -sDEVICE=pdfwrite -dFirstPage=150 -dLastPage=180 -c "[/CropBox [100 100 500 500]" -f in.pdf
复制代码

in case the pdf file has no CropBox property, which can be checked with
  1. pdfinfo in.pdf
复制代码

then the following command can be used instead
  1. gs -o cropped.pdf -sDEVICE=pdfwrite -dFirstPage=150 -dLastPage=180 -dDEVICEWIDTHPOINTS=200 -dDEVICEHEIGHTPOINTS=250 -dFIXEDMEDIA -c "<</PageOffset [-21 -32]>> setpagedevice" -f in.pdf
复制代码

3147

主题

8381

回帖

6万

积分

$\style{scale:11;fill:#eff}꩜$

积分
65357
QQ

显示全部楼层

 楼主| hbghlyj 发表于 2023-2-21 07:43

Compress PDF

Adobe Acrobat: Compress PDF online for free
Optimizing PDFs with Ghostscript By Ken Sharp - Thursday, September 29, 2022
How to compress PDF using ghostscript
I also added a function to my .bash_profile to make a shorthand that will compress and rename file.pdf to file.pdf.compressed.pdf:
  1. pdfcompress ()
  2. {
  3.    gs -q -dNOPAUSE -dBATCH -dSAFER -sDEVICE=pdfwrite -dCompatibilityLevel=1.3 -dPDFSETTINGS=/screen -dEmbedAllFonts=true -dSubsetFonts=true -dColorImageDownsampleType=/Bicubic -dColorImageResolution=144 -dGrayImageDownsampleType=/Bicubic -dGrayImageResolution=144 -dMonoImageDownsampleType=/Bicubic -dMonoImageResolution=144 -sOutputFile=$1.compressed.pdf $1;
  4. }
复制代码

use it: pdfcompress somefile.pdf

手机版|悠闲数学娱乐论坛(第3版)

GMT+8, 2025-3-4 21:05

Powered by Discuz!

× 快速回复 返回顶部 返回列表