Forgot password?
 Create new account
View 228|Reply 6

[Ghostscript]裁剪pdf页面

[Copy link]

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

hbghlyj Posted at 2022-5-18 00:56:45 |Read mode
Last edited by hbghlyj at 2022-8-11 19:11:00Linux下ghostscript的指令是gs, Windows下则是gswin32c.exe .(以下示例为linux)
首先用wget从Internet下载pdf文件到当前目录:
  1. wget http://pcjohnson.net/312/Sqrt.pdf
Copy the Code

提取第9页到outfile_9.pdf
  1. gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -dFirstPage=9  -dLastPage=9 -sOutputFile=outfile_9.pdf Sqrt.pdf
Copy the Code
得到:
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
Copy the Code
1.png
参考:stackoverflow.com/questions/6183479/cropping- … ing-ghostscript-9-01
再将pdf转换为svg
  1. pdftocairo -svg cropped.pdf Sqrt.svg
Copy the Code

$type Sqrt.svg (38.23 KB, Downloads: 116)

注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- … df/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.

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

 Author| hbghlyj Posted at 2022-5-18 02:03:11
本楼将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()
Copy the Code

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

 Author| hbghlyj Posted at 2022-5-18 02:17:02

[Javascript] async函数

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

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

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

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

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

 Author| hbghlyj Posted at 2022-8-12 02:07:22
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的维护得更好。

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

 Author| hbghlyj Posted at 2023-2-21 07:17:01
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
Copy the Code

in case the pdf file has no CropBox property, which can be checked with
  1. pdfinfo in.pdf
Copy the Code

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
Copy the Code

3146

Threads

8493

Posts

610K

Credits

Credits
66158
QQ

Show all posts

 Author| hbghlyj Posted at 2023-2-21 07:43:18

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. }
Copy the Code

use it: pdfcompress somefile.pdf

手机版Mobile version|Leisure Math Forum

2025-4-20 22:11 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list