raffaem / pdfsak

Utility to manipulate PDF files.
MIT License
43 stars 8 forks source link

pdftools require Python 3, you are using Python #2

Closed noloader closed 3 years ago

noloader commented 3 years ago

Hi Everyone,

I'm trying to add page numbers to a PDF per How to add page numbers to Postscript/PDF.

I used Wget to fetch pdftools and pdftools.py. They are in PWD with the files I want to modify.

Running pdftools.py --check-latex results in:

$  ./pdftools.py --check-latex
import-im6.q16: attempt to perform an operation not allowed by the security policy `PS' @ error/constitute.c/IsCoderAuthorized/408.
./pdftools.py: line 3: syntax error near unexpected token `'pdftools require Python 3, you are using Python ''
./pdftools.py: line 3: `    print('pdftools require Python 3, you are using Python ' + '.'.join(str(x) for x in sys.version_info[:3]))'

I'm not a Python person, so I can't help much with the Python3 problems. About all I know is Python3 is installed:

$ sudo apt-get install python3
Reading package lists... Done
Building dependency tree       
Reading state information... Done
python3 is already the newest version (3.8.2-0ubuntu2).

And:

$ command -v python3
/usr/bin/python3

The error messages don't make much sense to me. It would probably be helpful to tell the user what to do next.

raffaem commented 3 years ago
  1. Can you post all the output of running just python

  2. Can you try running the program with python3 ./pdftools.py --check-latex

noloader commented 3 years ago

It looks like the source file is missing a shebang... On Ubuntu 20, that means you get the default Python, which appears to be Python2.

--- pdftools.py
+++ pdftools.py
@@ -1,3 +1,5 @@
+#!/usr/bin/env python3
+
 import sys
 if sys.version_info[0] < 3:
     print('pdftools require Python 3, you are using Python ' + '.'.join(str(x) for x in sys.version_info[:3]))

And a quick touchup. On some of the BSDs, Bash is not located at /bin/bash. It is located at /usr/local/bin/bash (after the user manually installs it).

--- pdftools
+++ pdftools
@@ -1,4 +1,4 @@
-#!/bin/bash
+#!/usr/bin/env bash

 # see https://stackoverflow.com/questions/59895/how-to-get-the-source-directory-of-a-bash-script-from-within-the-script-itself

/usr/bin/env is Posix, so it is portable. It should not break your scripts. I've been using it for years and never had a problem. Also see https://stackoverflow.com/questions/6908143/should-i-put-shebang-in-python-scripts-and-what-form-should-it-take.

noloader commented 3 years ago

Are you sure this actually works:

pdftools --input-file input.pdf --output output.pdf --text $page/$pages 0.9 0.9 --fitpaper

I keep getting an error:

usage: pdftools.py [-h] [-if INPUT_FILES] [-id INPUT_DIRS]
                   [-o OUTPUT | --out-suffix OUT_SUFFIX] [--paper PAPER_TYPE]
                   [--scale SCALE_FACTOR] [--width WIDTH] [--height HEIGHT]
                   [--nup ROWS COLS] [--offset RIGHT TOP]
                   [--trim Left Bottom Right Top] [--delta X Y]
                   [--custom CUSTOM] [--pages PAGES]
                   [-t text_string anchor hpos vpos] [--text-help]
                   [--extract-pages file_name] [--natural-sorting]
                   [--overwrite] [--open] [--white-page] [--repair]
                   [--check-latex] [--split-pages] [--swap-pages]
                   [--last-page-even] [--clip] [--landscape] [--frame]
pdftools.py: error: argument -t/--text: expected 4 arguments

When I add an empty argument for anchor (what is an anchor?):

pdftools ... --text '$page/$pages' '""' 0.95 0.95 --fitpaper

I get another error:

usage: pdftools.py [-h] [-if INPUT_FILES] [-id INPUT_DIRS]
                   [-o OUTPUT | --out-suffix OUT_SUFFIX] [--paper PAPER_TYPE]
                   [--scale SCALE_FACTOR] [--width WIDTH] [--height HEIGHT]
                   [--nup ROWS COLS] [--offset RIGHT TOP]
                   [--trim Left Bottom Right Top] [--delta X Y]
                   [--custom CUSTOM] [--pages PAGES]
                   [-t text_string anchor hpos vpos] [--text-help]
                   [--extract-pages file_name] [--natural-sorting]
                   [--overwrite] [--open] [--white-page] [--repair]
                   [--check-latex] [--split-pages] [--swap-pages]
                   [--last-page-even] [--clip] [--landscape] [--frame]
pdftools.py: error: unrecognized arguments: --fitpaper
noloader commented 3 years ago

I'm going to find another way to add page numbers. This is not working out on Ubuntu 20.

Thanks for the help.

raffaem commented 3 years ago

@noloader

Hello,

Are you sure this actually works:

pdftools --input-file input.pdf --output output.pdf --text $page/$pages 0.9 0.9 --fitpaper

The info on StackOverflow is outdated. The CLI changed a little since then.

There is a new option to specify the anchor point of the text box which you want to position.

Please see the correct updated example file examples/addtext.bat

Relevant here is the section about the --text option in the help:

-t text_string anchor hpos vpos, --text text_string anchor hpos vpos
                        Add text to pdf file. 'text_string' is the string to
                        add, special variables can be passed, as well as LaTeX
                        font sizes like \Huge. Call --text-help for help on
                        how to build this string. 'anchor' is the point of the
                        text box (the box surrounding the text) to position:
                        'tl' will position the top-left corner, 'tr' will
                        position the top-right corner, 'bl' will position the
                        bottom-left corner, 'br' will position the bottom-
                        right corner, all other parameters are invalid. 'hpos'
                        and 'vpos' are numbers between 0 and 1 that represent
                        how far is 'anchor' from the top left corner of the
                        page. (default: None)

I am going to update the README.md and that StackOverflow post.

I am also going to add your suggestion for the base environment

When I add an empty argument for anchor (what is an anchor?):

Please see above:

'anchor' is the point of the text box (the box surrounding the text) to position: 'tl' will position the top-left corner, 'tr' will position the top-right corner, 'bl' will position the bottom-left corner, 'br' will position the bottom-right corner, all other parameters are invalid.

So the anchor specify the point of the text box to position. An empty anchor is not a valid option.

raffaem commented 3 years ago

Hello,

On Ubuntu you have to escape the $ sign in the shell.

So the actual correct example for you is this:

pdftools.py --input-file ./input/wikipedia_algorithm.pdf --output ./output/addtext.pdf --text "\$page/\$pages" br 1 1 --overwrite

As you can see from the screenshot, it is working. I am on Ubuntu 20.10

image

raffaem commented 3 years ago

Hello,

I have updated and successfully tested all the examples on Ubuntu 20.10

Please call pdftools.py directly:

pdftools.py .....

The shebang will call the correct interpreter.

There are no wrapper bash scripts anymore as they break when we pass float arguments.

Notice also that the --fitpaper option does not exist anymore as it is now the default.

raffaem commented 3 years ago

Reopen this if you need additional support.

All the exmaples work on Ubuntu 20.10 here, including adding page numbers.