web-dev-qa-db-ja.com

PDFをラスタライズしないようにコマンドラインからグレースケールに変換する方法は?

このPDFをグレースケールに変換しようとしています: https://dl.dropboxusercontent.com/u/10351891/page-27.pdf

Pdfwriteデバイスを使用したGhostscript(v 9.10)が失敗し、「色空間をグレーに変換できません。戦略をLeaveColorUnchangedに戻します。」メッセージ。

中間のpsファイル(gs、pdftops(v 0.24.3)またはpdf2psを使用)で変換できますが、この変換によりPDF全体がラスタライズされます。私は他にもたくさん試しました:PDF qpdf(v 5.0.1)またはpdftk(v 1.44)を使用して正規化し、それをsvgファイルに変換してからPDF Inkscape(v 0.48.4)経由...何も動作しないようです。

私が見つけた唯一の解決策(実稼働環境では私には適さない)は、Macでプレビューを使用して、手動またはAutomatorスクリプトでQuartz Gray Toneフィルターを適用することです。

誰かがそれを行う別の作業方法を見つけましたか?または、PDFを正規化するか、Ghostscriptのメッセージ「色空間を変換できません...」を回避するために問題を修正するか、別の方法で色空間を強制することは可能ですか?

ありがとう!

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

このコマンドは、ファイルをグレースケール(GS 9.10)に変換します。

37
user2846289

少し遅いですが、別のファイルを使用すると、トップの回答が機能しません。根本的な問題は、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
8
Reuben Thomas

最新のコード(まだリリースされていません)を使用し、ColorConversionStrategy = Grayを設定します

3
KenS

ファイルをクラックすると、ほとんどの色がRGB ICCベースの色空間によって決定されることがわかります(8 0 Rこの色空間へのすべての参照を検索します)。おそらくgsはそれについて不平を言っていますか?

知るか。

要点は、コンテンツに影響を与えずにページをある色空間から別の色空間に変換することは、ページをレンダリングして現在の色/色空間へのすべての変更をトラップし、ターゲット空間で同等のものを置き換えることができる必要があるという点で重要です。また、すべての画像XObjectを間違った色空間に変換します。これには、画像データをデコードしてターゲットスペースに再エンコードする必要があります。また、すべてのフォームXObjectも同様です。これは、親ページを変換しようとするのと同様のタスクです。 XObjectsフォーム(ドキュメントには4つあると思います)には、リソースと、ページマーキングオペレーターのコンテンツストリーム(より多くのXObjectsが含まれている場合があります)も含まれています。

それは確かに実行可能ですが、プロセスはレンダリングとほとんど同じですが、かなり特殊な目的のコードがいくつかあります。

2
plinth

非常に遅い応答ですが、次のコマンドが機能するはずです。

convert -colorspace GRAY input.pdf input_gray.pdf
1
Phyast10

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). 

ファイル実行許可を与える

chmod +x greypdf.sh

次のように実行します。

./greypdf.sh input.pdf

最初のファイルと同じ場所にinput-gray.pdfファイルを作成します

1
Salvi Pascual

私が作成したものを使用できます。グレースケールに変換する特定のページ番号を選択するオプションがあります。 PDF全体をグレースケールにしたくない場合に便利です。 https://github.com/shoaibkhan94/PdfGrayscaler

0
Shoaib Khan