Error in converting a PDF file page to PPM using pdftoppm - command-line

I am having trouble converting a PDF file to PPM using pdftoppm.exe. The PDF file has multiple pages, and I want to convert only page 6 of the file. I have tried the following but it inexplicably fails to do the conversion:
pdftoppm "C:/Users/lenovo/Desktop/folder1/502.pdf" "C:/Users/lenovo/Desktop/folder1/502" -png -f 6
pdftoppm "C:/Users/lenovo/Desktop/folder1/502.pdf" "C:/Users/lenovo/Desktop/folder1/502" -png -f 6 -l 6
pdftoppm "C:/Users/lenovo/Desktop/folder1/502.pdf" "C:/Users/lenovo/Desktop/folder1/502" -png -f 6 -l 6 -singlepage

After several minutes of trial and error, it appears the syntax I used was wrong. This worked for me:
pdftoppm -f 6 -l 6 "C:\Users\lenovo\Desktop\folder1\502.pdf" 502

Related

Command line pdftotext decimals don't work

I have a problem with pdftotext, in practice if in the specific command a decimal in the options "w" the reading of the pdf document does not work, here is the example of my command:
pdftotext -layout -y 714 -x 102 -W 14,39 -H 28 example.pdf pdf-example.txt
if, on the other hand, I delete the decimal figure from the command, everything works correctly.
I hope I have been clear enough.
Greetings

Open File keeps growing despite emptied content

How can I pipe a text stream into a file and, while the file is still in use, wipe it for job rotation?
Long version:
I've been struggling for a while onto an apparently minor issue, that's making my experiments impossible to continue.
I have a software collecting data continuously from external hardware (radiotelescope project) and storing in a csv format. Being the installation at a remote location I would, once a day, copy the saved data in a secure place and wipe the file content while, for the same reason, I can NOT to stop the hardware/software, thus software such as log rotation wouldn't be an option.
For as much effort spent see my previous post, it seems the wiped file keeps growing although empty.
Bizarre behavior, showing file size, truncate file, show file size again:
pi#tower /media/data $ ls -la radio.csv ;ls -s radio.csv;truncate radio.csv -s 1; ls -la radio.csv ;ls -s radio.csv
-rw-r--r-- 1 pi pi 994277 Jan 18 21:32 radio.csv
252 radio.csv
-rw-r--r-- 1 pi pi 1 Jan 18 21:32 radio.csv
0 radio.csv
Then, as soon as more data comes in:
pi#tower /media/data $ ls -la radio.csv ;ls -s radio.csv
-rw-r--r-- 1 pi pi 1011130 Jan 18 21:32 radio.csv
24 radio.csv
I thought to pipe the output into a sed command and save right away, with no luck altogether. Also, filesystem/hardware doesn't seems buggy (tried different hardware/distro/filesystem).
Would anyone be so nice to give me a hint how to proceed?
Thank you in advance.
Piped into tee with -a option. The file was kept open by originating source.
Option APPEND of tee helped to stick at the EOF new data; in the given issue, appending to the beginning when file zeroed.
For search engine and future reference:
sudo rtl_power -f 88M:108M:10k -g 1 - | tee -a radio.csv -
Now emptying the file with
echo -n > radio.csv
gets the file zeroed as expected.

GhostScript use bbox to crop Postscript file

What I am trying to accomplish is to crop my PostScript file called example.ps using the output described in bbox. I am doing this in a batch process where the bbox might be different for certain files. I have looked at pdfcrop and seen that it uses a similar approach. Here is the command I am using to crop right now.
gs -o cropped.pdf \
-sDEVICE=pdfwrite \
-dDEVICEWIDTHPOINTS=160 \
-dDEVICEHEIGHTPOINTS=840 \
-dFIXEDMEDIA \
-c "0 0 translate 0 0 160 840 rectclip" \
-f example.ps
The issue with this command is that I have to specify what width and height to use. What I want to happen is to some how call bbox first and then call this statement either through code or by using some command line redirection.
First, be aware that not every single page from a multi-page PostScript file will show the exact same "bounding box" values (in fact, this is rather rare). So you probably want to find out the common denominator across all possible bounding boxes (which would include them all).
Second, what you see in the console window when you run gs -sDEVICE=bbox is a mix of stdin and stdout output channels. However, the info you're after is going to stderr. If you redirect the command output to a file, you're capturing stdout, not stderr! To suppress some of the version and debugging info going to stderr add -q to the commandline.
So in order to get a 'clean* output of the bounding boxes for all pages, you have to re-direct the stderr channel first, which you then capture in file info.txt. So run a command like this (or similar):
gs \
-dBATCH \
-dNOPAUSE \
-q \
-sDEVICE=bbox \
example.ps \
2>&1 \
| tee info.txt
or even this, should you not need the info about the HiResBoundingBox:
gs \
-dBATCH \
-dNOPAUSE \
-q \
-sDEVICE=bbox \
example.ps \
2>&1 \
| grep ^%%Bound \
| tee info.txt
Also, BTW, note that can determine the bounding boxes of PostScript as well as PDF input files.
This should give you output like the following, where each line represents a page of the input file, starting with page 1 on the first line:
%%BoundingBox: 36 18 553 802
%%BoundingBox: 37 18 553 804
%%BoundingBox: 36 18 553 802
%%BoundingBox: 37 668 552 803
%%BoundingBox: 40 68 532 757
Lastly, you might want to read up in the following answers for some background info about Ghostscript's bbox device. You'll also find some alternative PostScript code for the cropping job there:
PDF - Remove White Margins
How to crop a section of a PDF file to PNG using Ghostscript

strip the last and first character from a String

Is fairly easy to strip the first and last character from a string using awk/sed?
Say I have this string
( 1 2 3 4 5 6 7 )
I would like to strip parentheses from it.
How should I do this?
sed way
$ echo '( 1 2 3 4 5 6 7 )' | sed 's/^.\(.*\).$/\1/'
1 2 3 4 5 6 7
awk way
$ echo '( 1 2 3 4 5 6 7 )' | awk '{print substr($0, 2, length($0) - 2)}'
1 2 3 4 5 6 7
POSIX sh way
$ var='( 1 2 3 4 5 6 7 )'; var="${var#?}"; var="${var%?}"; echo "$var"
1 2 3 4 5 6 7
bash way
$ var='( 1 2 3 4 5 6 7 )'; echo "${var:1: -1}"
1 2 3 4 5 6 7
If you use bash then use the bash way.
If not, prefer the posix-sh way. It is faster than loading sed or awk.
Other than that, you may also be doing other text processing, that you can combine with this, so depending on the rest of the script you may benefit using sed or awk in the end.
why doesn't this work? sed '..' s_res.temp > s_res.temp ?
This does not work, as the redirection > will truncate the file before it is read.
To solve this you have some choices:
what you really want to do is edit the file. sed is a stream editor not a file editor.
ed though, is a file editor (the standard one too!). So, use ed:
$ printf '%s\n' "%s/^.\(.*\).$/\1/" "." "wq" | ed s_res.temp
use a temporary file, and then mv it to replace the old one.
$ sed 's/^.\(.*\).$/\1/' s_res.temp > s_res.temp.temp
$ mv s_res.temp.temp s_res.temp
use -i option of sed. This only works with GNU-sed, as -i is not POSIX and GNU-only:
$ sed -i 's/^.\(.*\).$/\1/' s_res.temp
abuse the shell (not recommended really):
$ (rm test; sed 's/XXX/printf/' > test) < test
On Mac OS X (latest version 10.12 - Sierra) bash is stuck to version 3.2.57 which is quite old. One can always install bash using brew and get version 4.x which includes the substitutions needed for the above to work.
There is a collection of bash versions and respective changes, compiled on the bash-hackers wiki
To remove the first and last characters from a given string, I like this sed:
sed -e 's/^.//' -e 's/.$//'
# ^^ ^^
# first char last char
See an example:
sed -e 's/^.//' -e 's/.$//' <<< "(1 2 3 4 5 6 7)"
1 2 3 4 5 6 7
And also a perl way:
perl -pe 's/^.|.$//g'
If I want to remove the First (1) character and the last two (2) characters using sed.
Input "t2.large",
Output t2.large
sed -e 's/^.//' -e 's/..$//'
`

split a large text (xyz) database into x equal parts

I want to split a large text database (~10 million lines). I can use a command like
$ sed -i -e '4 s/(dB)//' -e '4 s/Best\ unit/Best_Unit/' -e '1,3 d' '/cygdrive/c/ Radio Mobile/Output/TRC_TestProcess/trc_longlands.txt'
$ split -l 1000000 /cygdrive/P/2012/Job_044_DM_Radio_Propogation/Working/FinalPropogation/TRC_Longlands/trc_longlands.txt 1
The first line is to clean the databse and the next is to split it -
but then the output files do not have the field names. How can I incorporate the field names into each dataset and pipe a list which has the original file, new file name and line numbers (from original file) in it. This is so that it can be used in the arcgis model to re-join the final simplified polygon datasets.
ALTERNATIVELY AND MORE USEFULLY -as this needs to go into a arcgis model, a python based solution is best. More details are in https://gis.stackexchange.com/questions/21420/large-point-to-polygon-by-buffer-join-buffer-dissolve-issues#comment29062_21420 and Remove specific lines from a large text file in python
SO GOING WITH A CYGWIN based Python solution as per answer by icyrock.com
we have process_text.sh
cd /cygdrive/P/2012/Job_044_DM_Radio_Propogation/Working/FinalPropogation/TRC_Longlands
mkdir processing
cp trc_longlands.txt processing/trc_longlands.txt
cd txt_processing
sed -i -e '4 s/(dB)//' -e '4 s/Best\ unit/Best_Unit/' -e '1,3 d' 'trc_longlands.txt'
split -l 1000000 trc_longlands.txt trc_longlands_
cat > a
h
1
2
3
4
5
6
7
8
9
^D
split -l 3
split -l 3 a 1
mv 1aa 21aa
for i in 1*; do head -n1 21aa|cat - $i > 2$i; done
for i in 21*; do echo ---- $i; cat $i; done
how can "TRC_Longlands" and the path be replaced with the input filename -in python we have %path%/%name for this.
in the last line is "do echo" necessary?
and this is called by python using
import os
os.system("process_text.bat")
where process_text.bat is basically
bash process_text.sh
I get the following error when run from dos...
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft
Corporation. All rights reserved.
C:\Users\georgec>bash
P:\2012\Job_044_DM_Radio_Propogation\Working\FinalPropogat
ion\TRC_Longlands\process_text.sh 'bash' is not recognized as an
internal or external command, operable program or batch file.
also when I run the bash command from cygwin -I get
georgec#ATGIS25
/cygdrive/P/2012/Job_044_DM_Radio_Propogation/Working/FinalPropogation/TRC_Longlands
$ bash process_text.sh : No such file or directory:
/cygdrive/P/2012/Job_044_DM_Radio_Propogation/Working/FinalPropogation/TRC_Longlands
cp: cannot create regular file `processing/trc_longlands.txt\r': No
such file or directory : No such file or directory: txt_processing :
No such file or directoryds.txt
but the files are created in the root directory.
why is there a "." after the directory name? how can they be given a .txt extension?
If you want to just prepend the first line of the original file to all but the first of the splits, you can do something like:
$ cat > a
h
1
2
3
4
5
6
7
^D
$ split -l 3
$ split -l 3 a 1
$ ls
1aa 1ab 1ac a
$ mv 1aa 21aa
$ for i in 1*; do head -n1 21aa|cat - $i > 2$i; done
$ for i in 21*; do echo ---- $i; cat $i; done
---- 21aa
h
1
2
---- 21ab
h
3
4
5
---- 21ac
h
6
7
Obviously, the first file will have one line less then the middle parts and the last part might be shorter, too, but if that's not a problem, this should work just fine. Of course, if your header has more lines, just change head -n1 to head -nX, X being the number of header lines.
Hope this helps.