PDFtk
PDFtk is one of the most popular and simple pdf editing or manipulating tool. It provides us a number of features like create pdf with FDF file, extracts records from the pdf and many more.
To use pdf on our local system we have to download it in our system using the command –
sudo apt-get install pdftk
But sometimes we are not able to install it while using shared hosting we are not allowed to install external tools but our system requirement is dependent on the tool. So, in that case –
We can use PDFtk binary files and point to that location instead of using installed tools on the system.
Steps To Make it Portable –
- Download its binary files – Here is the link https://github.com/lob/lambda-pdftk-example/tree/master/bin
- Place these files in your project
- Put these lines before using it in your python script
os.environ['LD_LIBRARY_PATH'] = os.path.dirname(os.path.abspath(__file__))+"/binary"
(Note – Here os.path.dirname(os.path.abspath(__file__))+”/binary” is the path where the files are placed like /var/www/html/PdftkTest/binary/ )
result = subprocess.run([os.environ['LD_LIBRARY_PATH']+'/pdftk', file_path, 'dump_data'], stdout=subprocess.PIPE)
For Example –
import os def main(): my_command = 'pdftk' os.path.dirname(os.path.abspath(__file__))+"/binary" #Location of PDFtk os.system(os.environ['LD_LIBRARY_PATH']+"/pdftk --version") #Print Version of PDFtk main()
Important Terms –
LD_LIBRARY_PATH – It is the search path environment variable for the Linux which are separated by (:) and where libraries are searched.
os.path.abspath(__file__) – It gives the complete path of the file.