Jpg to PDF in Ghostscript.

huangapple go评论57阅读模式
英文:

Jpg to PDf in Ghostscript

问题

抱歉,我的英文有限。据我所知,Ghostscript 不支持使用通配符 (*) 从命令行进行文件转换,所有文件必须在命令中明确列出(例如,0001.jpg, 0002.jpg)。您提供的两个脚本包含了通配符转换,但我无法找出为什么它们不起作用:

@echo off
if %1.==Sub. goto %2
for %%f in (F:\myFOLDER\*.jpg) do call %0 Sub action %%~nf
goto end
:action

"C:\Program Files (x86)\gs\gs10.01.1\bin\gswin32c.exe" -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=F:\myFOLDER\%3.pdf F:\myFOLDER\%3.jpg -c quit

:end
echo
dir="F:\myFOLDER"
output_file="F:\myFOLDER\out.pdf"
pause

args=""

for file in `find ${dir} -name "*.JPG" -o -name "*.JPEG" -o -name "*.jpg" -o -name "*.jpeg" -type f`;
do
  args="${args} (${file}) viewJPEG showpage"
done

"C:\Program Files (x86)\gs\gs10.01.1\bin\gswin32c.exe" -q -dNOSAFER -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=${output_file} "C:\Program Files (x86)\gs\gs10.01.1\lib\viewjpeg.ps" -c ${args}

我尝试根据互联网上的示例编辑代码,但我了解得还不够多。

英文:

Sorry my English. As far as I know, Ghostscript does not support file conversion by mask (*) from the command, and all files must be explicitly listed in command (0001.jpg, 0002.jpg). I came across such a two scripts containing mask conversion, but could not find out the reason why it does not work:

@echo off
if %1.==Sub. goto %2
for %%f in (F:\myFOLDER\*.jpg) do call %0 Sub action %%~nf
goto end
:action

"C:\Program Files (x86)\gs\gs10.01.1\bin\gswin32c.exe" -q -dNOPAUSE -sDEVICE=pdfwrite -sOutputFile=F:\myFOLDER\%3.pdf F:\myFOLDER\%3.jpg -c quit

:end
echo
dir="F:\myFOLDER"
output_file="F:\myFOLDER\out.pdf"
pause

args=""

for file in `find ${dir} -name "*.JPG" -o -name "*.JPEG" -o -name "*.jpg" -o -name "*.jpeg" -type f`;
do
  args="${args} (${file}) viewJPEG showpage"
done


"C:\Program Files (x86)\gs\gs10.01.1\bin\gswin32c.exe" -q -dNOSAFER -sPAPERSIZE=letter -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -sOutputFile=${output_file} "C:\Program Files (x86)\gs\gs10.01.1\lib\viewjpeg.ps" -c ${args}

I tried to edit the code using examples from the Internet, but I don't know enough.

答案1

得分: 0

有许多网络片段无法保证它们适用于其他人,只能简单地复制和粘贴。

首先,Ghostscript 只接受 pdf 作为输入和 jpg 作为输出,不接受反过来的方式,除非您通过被接受为输入的 Postscript 进行操作。

因此,它具有诸如避免文件名中包含空格字符等不安全字符的限制,如果必须运行可编程命令,只能使用已知的本地文件名进行操作。

因此,您需要让 GhostScript 使用一个会短暂闪现屏幕视图的查看器来呈现和打印 .jpg 文件(您不应该避免这个需求),然后将其转换为 PDF。

通过使用 NOSAFER,它将以当前用户的身份运行 Postscript,因此只能在安全的本地文件上运行已知的脚本.jpg。

注意
---
上述的 Windows 混合作为你的源文件的一部分只允许一个 jpg 转成一个 pdf。它故意使用窗口化而不是控制台版本(不是 gswin##C.exe)。
对于将多个 JPG 转成 PDF,姐妹产品 GhostPDL 或 MuTool 更适合这项任务,因为它们都可以直接接受 JPG 输入。
要使用上述脚本生成多页 PDF,您需要编写第二行来进行合并。
英文:

There are many web snippets that fail to ensure they work for other people that simply cut and paste.

So firstly Ghostscript only accepts pdf as input and jpg as output not the other way round, UNLESS, you go via Postscript which is accepted as input.

Hence that has limitations like avoid file names that have unsafe characters such as the space character and if you must run a programmable command only do it with known local filenames.

Thus you need to get GhostScript to render and print the .jpg's using a viewer that will briefly flash up screen view (you should not avoid that need) and convert that into PDF

By using NOSAFER it will run Postscript as current user so only run the known script on the safe local files.jpg

@echo off
set "GSBIN=C:\Program Files (x86)\gs\gs10.01.1\bin"
set "FOLDER=F:\myFOLDER"

cd /d "%folder%"
for %%f in (*.jpg) do %GSBIN%\gswin32 -sDEVICE=pdfwrite -dNOSAFER -o "%%~nf.pdf" "%GSBIN%\..\lib\viewjpeg.ps" -c "(""%%~f"") viewJPEG" -f
cd /d "%~dp0"

NOTE

The above Windows mix as part of both your sources will only allow one jpg per pdf. It is intentionally using Windowed NOT Console (NOT gswin##C.exe) version
For Multiple JPG to PDF the sister ArtifeX products GhostPDL or MuTool would be better suited to that task, as they both accept JPG inputs direct.
To produce multipage PDF with above script you would need to write a second line to do merging.

huangapple
  • 本文由 发表于 2023年4月11日 12:23:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75982396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定