I have a large number of PDF files that I need to convert to EPS. I'm using this command to do that:
gs -dNOCACHE -dNOPAUSE -dBATCH -dSAFER -sDEVICE=eps2write -dEPSCrop -sOutputFile=output.eps input.pdf
That converts individual files, but I have a large number of them. How can I convert many pdf files into many eps files, keeping the names before the file extension? I'm using zsh on a mac.
I am using a for-loop, but I'd have to rename all the files:
for i in *.pdf
do
gs -dNOCACHE -dNOPAUSE -dBATCH -dSAFER -sDEVICE=eps2write -dEPSCrop -sOutputFile=$i.eps $i
done
results in output files having extension .pdf.eps, which I then rename with
zmv '(*)(.pdf).eps' '$1.eps'
Related
i want to convert all pdfs of folder to png images,like if i have two pdfs in folder name test1.pdf and test2.pdf with two pages,then it should generate test1-1.png ,test1-2.png,test2-1.png,test2-2.png.
I am using this command line ,what should i place instead of '#-%d.png'?
gs -dNOPAUSE -dBATCH -sDEVICE=png16m -sOutputFile="/var/www/pdf_png/pdfs/#-%d.png" /var/www/pdf_png/pdfs/*.pdf
You can't do it using Ghostscript alone, you need to write a shell script to invoke Ghostscript on each source file.
This question has a script in it that you may be able to adapt for your purposes
find . -type f -iname "*.pdf" -exec sh -c 'gs -dNOPAUSE -dBATCH -sDEVICE=pngalpha -r300 -sOutputFile=$1.png $1' x {} \;
Note: This will still output FILENAME.pdf.png, but hey ... it's a one liner ...
Explanation
find command finds all pdf files and passes them to sh via -exec
the {} represents the file name, but in order to be used twice in the command it has to be passed with the sh -c trick as a positional argument
in case you are wondering the character x is passed as the $0 argument because convetionally $0 is the shellname
\; is the character used to signal the end of the command
I use pandoc to convert docx to markdown with the following:
pandoc -f docx -t markdown --extract-media="pandoc-output/$filename/" -o "pandoc-output/$filename/full.md" "$fullfile"
Which works OK. However, the media is stored in:
pandoc-output/$filename/media/
I want the media to be stored in
/pandoc-output/media/$filename/
Is this possible?
UPDATE
I ended up with a sed command to search and replace the offending lines together with a mv to the proper directory.
gsed -i -r "s/([a-zA-Z0-9_-]+)\/pandoc-output\/media\/([a-zA-Z0-9]+)/\/public\/media\/\1\/\2/" $ROOTDIR"$d"_"$filename.html.md"
At the moment I'm using this command to convert file X.pdf to X.tif .
gs -q -sDEVICE=tiffg4 -r300 -dBATCH -dPDFFitPage -dNOPAUSE -sOutputFile=tif/X.tif pdf/X.pdf
Is there a smooth way to do the equivalent of
gs -q -sDEVICE=tiffg4 -r300 -dBATCH -dPDFFitPage -dNOPAUSE -sOutputFile=tif/*.tif pdf/*.pdf as one would do with say search queries? (I replaced the X with *) It obviously doesn't work with this method, but maybe there is a similiar syntax?
Or am I going to have to write a batch file or something like that?
PS: Im on OSX
Try saving this as tif2pdf in the directory containing subdirectories tif and pdf:
#!/bin/bash
# Change into the "tif" directory to find the input files
cd tif || { echo ERROR: Subdirectory tif not found; exit 1; }
# Loop through all files ending in ".tif"
for f in *.tif; do
# Determine output filename
out=${f%%tif}
out="../pdf/${out}pdf"
# Show the command we would run
echo gs -q -sDEVICE=tiffg4 -r300 -dBATCH -dPDFFitPage -dNOPAUSE -sOutputFile="$out" "$f"
done
Then go in that directory in Terminal and make this script executable with:
chmod +x tif2pdf
Then run it with:
./tif2pdf
At the moment, it does nothing, except show you what it would do. If you like the appearance of the commands it makes up, edit the script and remove the word echo in the penultimate line and run it again to actually do the conversions.
Sample Output
gs -q -sDEVICE=tiffg4 -r300 -dBATCH -dPDFFitPage -dNOPAUSE -sOutputFile=../pdf/a.pdf a.tif
gs -q -sDEVICE=tiffg4 -r300 -dBATCH -dPDFFitPage -dNOPAUSE -sOutputFile=../pdf/b.pdf b.tif
I suggest running it on a COPY of your files first.
I have a tar archive which contains several text files. I would like to write a script to display (stdout) the content of a file without extracting it to the current directory.
Actually I would like to do the same as:
tar tf myArchive.tar folder/someFile.txt
cat folder/someFile.txt
rm -R folder
but without the rm...
I tried this way but it didn't work:
tar tf myArchive.tar folder/someFile.txt | cat
Thanks
Use x to extract, with f from archive file. Then add also option -O to direct extracted files to standard output.
tar xf myArchive.tar folder/someFile.txt -O
I use the script below to convert all pdf files in a directory to png files, and I want to run it only over the files that have yet to be converted.
#!/bin/bash
# Convert pdf to png
for f in *.pdf
do
echo "Converting $f"
gs -dNOPAUSE -dBATCH -sDEVICE=png256 -r480 -q -sOutputFile="png/$f.png" "$f"
done
rename -f 's/\.pdf\.png/\.png/' png/*.pdf.png
How do I modify the loop so that it is restricted to files where the corresponding png file either does not exist or is older than the pdf file?
A simple modification to your script:
#!/bin/bash
# Convert pdf to png
for f in *.pdf
do
png="png/${f%pdf}png"
if [ -e "$png" -a "$f" -nt "$png" ]; then
continue
fi
echo "Converting $f"
gs -dNOPAUSE -dBATCH -sDEVICE=png256 -r480 -q -sOutputFile="$png" "$f"
done
Firstly, we create a $png variable using in place editing. Basically the %pdf tells bash to remove the last occurrence of pdf, which is the extension. Then we can place this into a string that prefixes it with png/ and adds the png extension. This saves you the last rename command.
Now we have an if statement that continues the loop if a certain condition is met. Continue means go to the next iteration of the loop, without executing anything else for this iteration. The condition is "$png" exists (-e "$png") and (-a) "$f" is newer than "$png" ("$f" -nt "$png").
But I would suggest writing a Makefile:
PDFS := $(wildcard *.pdf)
PNGS := $(addprefix png/,$(PDFS:.pdf=.png))
all: $(PNGS)
png/%.png: %.pdf
gs -dNOPAUSE -dBATCH -sDEVICE=png256 -r480 -q -sOutputFile="$#" "$<"
And run it with make any time you want to generate newer PNGs.