2013-11-21 45 views
14

我想转换为灰度此PDF:https://dl.dropboxusercontent.com/u/10351891/page-27.pdf如何将PDF转换为灰度从命令行避免被光栅化?

的Ghostscript(V 9.10)与pdfwrite设备失败,出现“无法色彩空间转换为灰色,恢复策略,以LeaveColorUnchanged。”信息。

我能够通过中介PS文件,将其转换(使用GS,pdftops(V 0.24.3)或pdf2ps),但这皈依栅格化整个PDF。 我尝试了很多其他的东西:使用qpdf(v 5.0.1)或pdftk(v 1.44)对PDF进行标准化,将其转换为svg文件并通过Inkscape返回到PDF(v 0.48.4)...没有任何东西似乎工作。

唯一的一个解决方案,我发现(这是不适合我在生产环境中)是用我的Mac上预览和手动或使用的Automator脚本应用石英灰色调的过滤器。

任何人找到另一种工作方式来做到这一点? 还是有可能规范化PDF或解决问题,以防止Ghostscript消息“无法转换颜色空间...”或以另一种方式强制颜色空间?

谢谢!

+0

[超级用户讨论](https://superuser.com/q/318586/45163) – palswim

回答

26
gs \ 
    -sDEVICE=pdfwrite \ 
    -sProcessColorModel=DeviceGray \ 
    -sColorConversionStrategy=Gray \ 
    -dOverrideICC \ 
    -o out.pdf \ 
    -f page-27.pdf 

这个命令转换您的文件为灰度(GS 9.10)。

+0

惊人的,它的作品就像一个魅力!谢谢! – Panda

+2

它给出了这个输出,PDF仍然是彩色的。 GPL Ghostscript 9.10: 无法将颜色空间转换为灰色,将策略恢复为LeaveColorUnchanged。 – Gaurav

+0

+1在我的OSX上开箱即用。添加$ 1和$ 2而不是out.pdf和page-27.pdf,并将其转换为批处理脚本,这对于每天使用更灵活也很有帮助。 –

2

如果您破解文件,您会发现大多数颜色是通过基于RGB ICC的颜色空间确定的(查找8 0 R以查找对此颜色空间的所有引用)。也许gs在抱怨?

谁知道。

带走的是,将页面从一个颜色空间转换为另一个颜色空间而不影响内容是非平凡的,因为您需要能够呈现页面并捕获对当前颜色/颜色空间的所有更改并替换等效在目标空间以及将所有图像XObjects转换为错误的颜色空间,这将需要对图像数据进行解码并将其重新编码到目标空间中,以及所有形式的XObjects,这将是类似于尝试转换的任务从表单XObjects(我认为你的文档有4)开始的父页面也包含页面标记操作符(可能包含更多XObjects)的资源和内容流。

这肯定是可行的,但过程是几乎相同的渲染但也有一些相当特殊用途的代码。

3

使用最新的代码(尚未公布),并设置ColorConversionStrategy =灰色

7

今天晚了一点,但最好的答案不适用于我与一个不同的文件。底层问题似乎是Ghostscript中的旧代码,对于这些代码,有一个默认情况下未启用的更高版本。更多的是在这里:http://bugs.ghostscript.com/show_bug.cgi?id=694608

上面也该页面给出了对我的作品的命令:

gs -sDEVICE=pdfwrite -dProcessColorModel=/DeviceGray -dColorConversionStrategy=/Gray -dPDFUseOldCMS=false -o out.pdf -f in.pdf 
1

在Linux中:

安装PDFTK

apt-get install pdftk 

一旦你已经安装PDFTK ,将脚本保存为graypdf。SH用下面的代码

# convert pdf to grayscale, preserving metadata 
# "AFAIK graphicx has no feature for manipulating colorspaces. " http://groups.google.com/group/latexusersgroup/browse_thread/thread/5ebbc3ff9978af05 
# "> Is there an easy (or just standard) way with pdflatex to do a > conversion from color to grayscale when a PDF file is generated? No." ... "If you want to convert a multipage document then you better have pdftops from the xpdf suite installed because Ghostscript's pdf to ps doesn't produce nice Postscript." http://osdir.com/ml/tex.pdftex/2008-05/msg00006.html 
# "Converting a color EPS to grayscale" - http://en.wikibooks.org/wiki/LaTeX/Importing_Graphics 
# "\usepackage[monochrome]{color} .. I don't know of a neat automatic conversion to monochrome (there might be such a thing) although there was something in Tugboat a while back about mapping colors on the fly. I would probably make monochrome versions of the pictures, and name them consistently. Then conditionally load each one" http://newsgroups.derkeiler.com/Archive/Comp/comp.text.tex/2005-08/msg01864.html 
# "Here comes optional.sty. By adding \usepackage{optional} ... \opt{color}{\includegraphics[width=0.4\textwidth]{intro/benzoCompounds_color}} \opt{grayscale}{\includegraphics[width=0.4\textwidth]{intro/benzoCompounds}} " - http://chem-bla-ics.blogspot.com/2008/01/my-phd-thesis-in-color-and-grayscale.html 
# with gs: 
# http://handyfloss.net/2008.09/making-a-pdf-grayscale-with-ghostscript/ 
# note - this strips metadata! so: 
# http://etutorials.org/Linux+systems/pdf+hacks/Chapter+5.+Manipulating+PDF+Files/Hack+64+Get+and+Set+PDF+Metadata/ 
COLORFILENAME=$1 
OVERWRITE=$2 
FNAME=${COLORFILENAME%.pdf} 
# NOTE: pdftk does not work with logical page numbers/pagination; 
# gs kills it as well; 
# so check for existence of 'pdfmarks' file in calling dir; 
# if there, use it to correct gs logical pagination 
# for example, see 
# http://askubuntu.com/questions/32048/renumber-pages-of-a-pdf/65894#65894 
PDFMARKS= 
if [ -e pdfmarks ] ; then 
PDFMARKS="pdfmarks" 
echo "$PDFMARKS exists, using..." 
# convert to gray pdf - this strips metadata! 
gs -sOutputFile=$FNAME-gs-gray.pdf -sDEVICE=pdfwrite \ 
-sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray \ 
-dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH "$COLORFILENAME" "$PDFMARKS" 
else # not really needed ?! 
gs -sOutputFile=$FNAME-gs-gray.pdf -sDEVICE=pdfwrite \ 
-sColorConversionStrategy=Gray -dProcessColorModel=/DeviceGray \ 
-dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH "$COLORFILENAME" 
fi 
# dump metadata from original color pdf 
## pdftk $COLORFILENAME dump_data output $FNAME.data.txt 
# also: pdfinfo -meta $COLORFILENAME 
# grep to avoid BookmarkTitle/Level/PageNumber: 
pdftk $COLORFILENAME dump_data output | grep 'Info\|Pdf' > $FNAME.data.txt 
# "pdftk can take a plain-text file of these same key/value pairs and update a PDF's Info dictionary to match. Currently, it does not update the PDF's XMP stream." 
pdftk $FNAME-gs-gray.pdf update_info $FNAME.data.txt output $FNAME-gray.pdf 
# (http://wiki.creativecommons.org/XMP_Implementations : Exempi ... allows reading/writing XMP metadata for various file formats, including PDF ...) 
# clean up 
rm $FNAME-gs-gray.pdf 
rm $FNAME.data.txt 
if [ "$OVERWRITE" == "y" ] ; then 
echo "Overwriting $COLORFILENAME..." 
mv $FNAME-gray.pdf $COLORFILENAME 
fi 
# BUT NOTE: 
# Mixing TEX & PostScript : The GEX Model - http://www.tug.org/TUGboat/Articles/tb21-3/tb68kost.pdf 
# VTEX is a (commercial) extended version of TEX, sold by MicroPress, Inc. Free versions of VTEX have recently been made available, that work under OS/2 and Linux. This paper describes GEX, a fast fully-integrated PostScript interpreter which functions as part of the VTEX code-generator. Unless specified otherwise, this article describes the functionality in the free- ware version of the VTEX compiler, as available on CTAN sites in systems/vtex. 
# GEX is a graphics counterpart to TEX. .. Since GEX may exercise subtle influence on TEX (load fonts, or change TEX registers), GEX is op- tional in VTEX implementations: the default oper- ation of the program is with GEX off; it is enabled by a command-line switch. 
# \includegraphics[width=1.3in, colorspace=grayscale 256]{macaw.jpg} 
# http://mail.tug.org/texlive/Contents/live/texmf-dist/doc/generic/FAQ-en/html/FAQ-TeXsystems.html 
# A free version of the commercial VTeX extended TeX system is available for use under Linux, which among other things specialises in direct production of PDF from (La)TeX input. Sadly, it���s no longer supported, and the ready-built images are made for use with a rather ancient Linux kernel. 
# NOTE: another way to capture metadata; if converting via ghostscript: 
# http://compgroups.net/comp.text.pdf/How-to-specify-metadata-using-Ghostscript 
# first: 
# grep -a 'Keywo' orig.pdf 
# /Author(xxx)/Title(ttt)/Subject()/Creator(LaTeX)/Producer(pdfTeX-1.40.12)/Keywords(kkkk) 
# then - copy this data in a file prologue.ini: 
#/pdfmark where {pop} {userdict /pdfmark /cleartomark load put} ifelse 
#[/Author(xxx) 
#/Title(ttt) 
#/Subject() 
#/Creator(LaTeX with hyperref package + gs w/ prologue) 
#/Producer(pdfTeX-1.40.12) 
#/Keywords(kkkk) 
#/DOCINFO pdfmark 
# 
# finally, call gs on the orig file, 
# asking to process pdfmarks in prologue.ini: 
# gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 \ 
# -dPDFSETTINGS=/screen -dNOPAUSE -dQUIET -dBATCH -dDOPDFMARKS \ 
# -sOutputFile=out.pdf in.pdf prologue.ini 
# then the metadata will be in output too (which is stripped otherwise; 
# note bookmarks are preserved, however). 

给文件excecution权限

chmod +x greypdf.sh 

而且像这样执行:

./greypdf.sh input.pdf 

它会在同一个创建文件输入gray.pdf位置比最初的文件

+0

这似乎不是真正的灰度。如果我在它上面运行inkcov,它使用C,M和Y来制作灰色。 –

1

响应很迟,但下面的命令应该可以工作:

convert -colorspace GRAY input.pdf input_gray.pdf 
+0

它将矢量转换为光栅图像。 – brownian

0

你可以使用我创建的东西。它使您可以选择要转换为灰度的特定页码。如果您不想灰度化整个pdf,方便使用。 https://github.com/shoaibkhan94/PdfGrayscaler

+0

请不要对两个不同的问题使用相同的答案。相反,回答一个问题,并标记第二个添加重复。 – mkl

相关问题