Can Ghostscript start numbering pages from zero? - ghostscript

I am using Ghostscript to convert a multi-page PDF to individual JPEG files and can get it to output the files numbered like page_%03d.jpg.
But it always starts at page_001.jpg and I need it to start numbering the output files starting from page_000.jpg.
Is there a setting I can use to get Ghostscript to start at zero or am I going to have to rename all the files after processing?

Hmm... tricky question. I don't think there is a way to tweak the -sOutputFile=string_%03d.jpeg-syntax to start at zero.
However, what about trying it with a little workaround?
The trick is to use 2 passes for processing your PDF file
First pass: make processing by Ghostscript start with page 2 through the end. Your page numbering for this pass will still start at 1. But each consecutive page will now have a filename which is offset by -1.
Second pass: make processing by Ghostscript stop after page 1, and hardcode the output filename to include your desired zero numbering.
Here is are the two commands spelled out explicitely:
First pass:
gswin32c.exe ^
-o c:/path/to/output/page_%03d.jpg ^
-sDEVICE=jpeg ^
[...more options as needed...] ^
-dFirstPage=2 ^
-f c:/path/to/input.pdf
This will result in:
first page processed, page 2 ....... named as page_001.jpg
second page processed, page 3 ....... named as page_002.jpg
third page processed, page 4 ....... named as page_003.jpg
[...]
Second pass:
gswin32c.exe ^
-o c:/path/to/output/page_000.jpg ^
-sDEVICE=jpeg ^
[...more options as needed...] ^
-dLastPage=1 ^
-f c:/path/to/input.pdf
This will result in:
only page processed, page 1 ....... named as page_000.jpg
Voila!
This little trick can spare you a lot of work renaming all the pages. It's surely faster as soon as you have more than a few pages to process. And of course, this basic approach can easily be scripted.
Enjoy...
☺

To close this question, i will answer myself: no ghostscript cannot start numbering from zero. I had to rename all the files after ghostscript was done processing.

Related

What is the syntax when giving data to an option of a terminal program?

wkhtmltopdf tool convert html document to pdfs. But I want to specify the left margin. What should the syntax be?
$ wkhtmltopdf --margin-left=10 ex1.html ex1.pdf
Unknown long argument --margin-left=10
Every terminal program may vary in its syntax a bit. Try
wkhtmltopdf --help
To see if there's a commands list.
Also found a manual https://wkhtmltopdf.org/usage/wkhtmltopdf.txt when searched for it. Suggests that the syntax is:
wkhtmltopdf --margin-left 10mm
All I needed was to give 1 space between the option and the value and 2 spaces for the next option
wkhtmltopdf --margin-left 10 --margin-right 10 ex1.html ex1.pdf

Using cloc (count Lines of Codes) result

I am writing a script for my research, and I want to get the total number of lines in a source file. I came around cloc and I think I am going to use it in my script.
However, cloc gives result with too many information (unfortunately since I am a new member I cannot upload a photo). It gives number of files, number of lines, number of blank lines, number of comment lines, and other graphical representation stuff.
I am only interested in the number of lines to use it on my calculations. Is there a way to get that number easily (maybe by performing some options in command line (although I went through the available options and didn't find something useful for my case))?
I thought to do regular expression on the result to get the number; however, this is my first time using cloc and there might be a better/professional way of doing it.
Any thought?
Regards,
Arwa
I am not sure about CLOC. But it is worth using default shell command.
Please have a look at this question.
To get number of lines of code individually
find . -name '*.*' | xargs wc -l
To get total number of lines of code in a directory.
(find ./ -name '*.*' -print0 | xargs -0 cat) | wc -l
Please note that if you need number of lines from files with specific extension you could use *.ext. *.rb, if it is ruby.
For something very quick and simple you could just use:
Dir.glob('your_directory/**/*.rb').map do |file|
File.foreach(file).count
end.reduce(:+)
This will count all the lines of .rb files in your_directory and it's sub directories. Although I would recommend adding some handling for blank lines as well as comment lines. For more on Dir::glob
#BinaryMee and #engineersmnky thanks for your response.
I tried two different solutions, one using "readlines" got the answer from #gicappa
Count the length (number of lines) of a CSV file?
the other solution using cloc. I ran the command
%x{perl #{ClocPath} #{path-to-file} > result.txt}
and saved the result in result.txt
cloc returns result in a graphical form (I cannot upload image), it also reports number of blank lines, comment lines, and code lines. As I said, I am interested in code lines. So, I opened the file and used regular expression to get the number I needed.
content = File.read("#{path}/result.txt")
line = content.scan(/(\s+\d+\s+\d+\s+\d+\s+\d+)/)
total = line[0][0].split(' ').last
content here will have the content of a file, then line will get this line from the file:
C# 1 3 3 17
C# is the language of a file, 1 is number of files, 3 is number of blank lines, 3 is number of comment lines, and 17 is number of code lines. I got the help of the format from the script of cloc. total then will have number 17.
This solution will help if you are reading a specific file only, you need to add more solutions if you are reading the lines of more than one file.
Hopefully this will help who needs it.
Regards,
Arwa

PDF to JPEG conversion using Ghostscript

I'm using Ghostscript to convert my PDF files to JPEGs with Ghostscript which works great.
For my output images I'm using %03d in the file name, so the file names come out 001, 002 ... and so on according to the page numbers.
But i want in some case the numbers to start from an higher number.
For example I process a file with two pages so the output images are page001.jpg, page002.jpg
Now I want to process another PDF and instead of replacing those files, I want to create page003.jpg, page004.jpg.
How can this be done?
This is my full command line I'm using now:
'C:\gs\gs9.14\bin \gswin64c -dNOPAUSE -sDEVICE=png16m \
-sOutputFile=page-%03d.jpg -r100x100 -q' . $pdf_file. '-c quit'
Here is a workaround trick, that you could use:
gswin64c.exe ^
-sDEVICE=png16m ^
-sOutputFile=page-%03d.jpg ^
-r100x100 ^
-c "showpage showpage" ^
-f filename.pdf
The -c "showpage showpage" inserts two empty pages into the output. The output files will be named
page-001.jpg + page-002.jpg + page-003.jpg + page-004.jpg
So the first two are white-only JPEGs and should be deleted afterwards.
You can extend this command with any number of empty pages you want.
Update
Of course, if you know in advance that you want to convert several different PDF files to images where you want the counting for a new PDF to continue exactly from where the last PDF ended, you could do this:
gswin64c.exe ^
-sDEVICE=jpeg ^
-sOutputFile=page-%03d.jpg ^
-r100x100 ^
-f file1.pdf ^
-f file2.pdf ^
-f file3.pdf ^
-f [...]
BTW, your original command requests .jpg file suffixes, while the Ghostscript device is png16m. This doesn't match. Initially I blindly copied your command, but now I've corrected it.
You cannot do that with the standard version of Ghostscript, the output file numbers are given as the emitted page number (so if you had a 10 page file, with /NumCOpies 2, you would get files numbered 0 to 19).
Of course, you can process the two files on the same command line, I think that will give you the second file with page numbers beginning where the first set left off.
Otherwise you will have to modify the source code of the Ghostscript device.

invalid character name at (1)

I am trying to compile a fortran code. It will analyze an X file in an Y directory and then create a new file Z with the results. But there is something wrong occurring.
When I write the directory I see that it is too much for one line and then I try to continue it in the next one doing this:
namech='/home/matheus/Documents/UFABC/IC/Spectra/Elliptical/'
+ 'espec.fits'
But, when I try to compile using the command
gfortran Codigo.f -o TESTE -Lcfitsio -lcfitsio
I get this error message:
+ 'espec.fits'
1
Error: Invalid character in name at (1)
Can someone help me? Actually I do not know what this error is. The directory is 100% right. And when I move the archives to a simpler directory to be able to write everything in one line, it works! So is there something wrong with the "+"?
Thank you.
Edit1
Actually, when I add "&" in the end of the line, it gives me this error message:
namech='/home/matheus/Documents/UFABC/IC/Spectra/Elliptical/'&
1
Error: Unclassifiable statement at (1)
Codigo.f:60.7:
+ 'espec.fits'
1
Error: Invalid character in name at (1)
And with "//":
namech='/home/matheus/Documents/UFABC/IC/Spectra/Elliptical/'//
1
Error: Syntax error in expression at (1)
Codigo.f:60.7:
+ 'espec.fits'
1
Error: Invalid character in name at (1)
Edit2
Thank you so much for helping me. Well, I solved the problem switching to the ".f90" form.
Just one more question: do you know why it does not recognize the "c" for comments in the code? Thank you again! :)
This part of your compilation statement:
gfortran Codigo.f
will treat the source file, with its .f suffix, as fixed form source. This means that a continuation line is indicated by any character (other than a blank or a 0) in column 6.
However, the error message you get suggests that the + in the second line of your snippet is not in column 6 and that the compiler is treating it as the initial character in a new entity name for which it is not valid. The fact that the + is aligned, vertically, with n in the previous line strengthens my suspicion that this may the root of your problem.
Adding the ampersand, as suggested in a now-deleted answer, doesn't actually help in this case if you continue to tell the compiler that it is dealing with a fixed form source file. & is only used for continuation in free form source files. Adding the string-concatenation operator, //, doesn't help either since it is not followed by another string but a line ending. //& would help but is probably unnecessary.
I think you have 2 possible solutions, but choose only one:
Stick with fixed form and get the alignment right.
Change the file suffix to .f90 which will cause gfortran to treat the source file as free-form.
If you go for option 2 (which I would recommend) you can then either use & at the end of the continued line or you could simply merge the lines. In free-form the maximum line length is 132 characters.
Adding to High Performance Mark's answer:
If you continue with FORTRAN 77, most compilers have an option to increase the allowed line length, e.g., -ffixed-form -ffixed-line-length-none for gfortran. As already stated, Fortran >=90 has line length of 132, so you wouldn't need to split the line.
Finally, if you want to split the line in Fortran >=90, you need two ampersands. In most cases you need one, but to split a string you need two:
namech='/home/matheus/Documents/UFABC/IC/Spectra/Elliptical/&
&espec.fits'

Ghostscript PDF to PNG: output is always 595x842 (A4)

I try to convert PDF to PNG, but ouput image is always A4, however the source PDF is very huge. Here are my commands:
-dNOPAUSE ^
-dBATCH ^
-dSAFER ^
-sDEVICE=png16m ^
-dFirstPage=1 ^
-sOutputFile="D:\PDF.png" ^
"D:\PDF.pdf" ^
-sPAPERSIZE=a1
I tried several options (-r, -g, -sDEFAULTPAPERSIZE), but none worked.
How can I force the output image dimensions?
P.S: my PDF file
Your linked-to PDF file has only 1 page. That means your commandline parameter -dFirstPage=1 doesn't have any influence.
Also, your -sPAPERSIZE=a1 parameter should not be last (it doesn't have any influence here -- so Ghostscript takes the default size from the pagesize of the input PDF, which is A4). Instead it should appear somewhere before the "D:\PDF.pdf" (which must be last).
It looks like you want a PNG with the size of A1, and your OS is Windows (guessing from the partial commandline you provided)?
Try this instead (it adds -dPDFFitPage=true to the commandline and puts the arguments into a correct order, while also shortening it a bit using the -o trick):
gswin32c.exe ^
-o "D:\PDF.png ^
-sDEVICE=png16m ^
-sPAPERSIZE=a1 ^
-dPDFFitPage=true ^
"D:\PDF.pdf"
This should give you a PNG with the size of 1684x2384 pixel at 72dpi (which is the builtin default for all Ghostscript image output, used if no other resolution is specified). For different combinations of resolution and pagesize add your variation of -rXXX and -gNNNxMMM (instead of -sPAPERSIZE=a1) but by all means keep the -dPDFFitPage=true....
You can also keep the -sPAPERSIZE=a1 and add -r100 or -r36 or -r200 if you want a different resolution only. Be aware that increasing resolution may not improve the image quality compared to the default output of 72dpi. That depends on the resolution of the images that were embedded in the PDF page. But it surely increases the file size...
function pdf2png-mutool() {
#: "mutool draw [options] file [pages]"
# pages: Comma separated list of page numbers and ranges (for example: 1,5,10-15,20-N), where
# the character N denotes the last page. If no pages are specified, then all pages
# will be included.
local i="$1"
local out="${pdf2png_o:-${i:r}_%03d.png}"
[[ "$out" == *.png ]] || out+='.png'
command mutool draw -o "$out" -F png "$i" "${#[2,-1]}"
#: '`-r 300` to set dpi'
}

Resources