• Contact

Sudeep's Blog – The Programming Adventure

~ code is poetry

Tag Archives: convert

Convert the list of images to PDF file and add watermark in Ubuntu

24 Thursday Nov 2016

Posted by Sudeep Acharya in Python, Ubuntu

≈ Leave a comment

Tags

convert, images, imagetopdf, python, ubuntu 16.04

Converting list of images to PDF in Ubuntu
Let’s use ImageMagick tool. Normally it is installed in Ubuntu. If you need to install it then run:

$ sudo apt-get install imagemagick

Then you can convert it by using:
$ convert image_1.jpg image_2.jpg output.pdf

You can specify any numbers of images but the last argument must be a name of your PDF file along with extension (.pdf).

Adding the watermark in the PDF file
We use Python script for adding watermark to each page in the PDF file. We will be using “PyPDF2” python library.

$ pip install PyPDF2

Make your watermark ready, convert it to PDF file. Make sure that watermark file is of same page size as of your PDF file.

Then run this code by modifying the file names:

import PyPDF2

originalFile = open('inputfile_pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(originalFile)
pdfWriter = PyPDF2.PdfFileWriter()

for i in range(0, pdfReader.numPages):
    originalfile_page = pdfReader.getPage(i)
    pdfWatermarkReader = PyPDF2.PdfFileReader(open('watermark.pdf', 'rb'))
    originalfile_page.mergePage(pdfWatermarkReader.getPage(0))
    pdfWriter.addPage(originalfile_page)

resultPdfFile = open('watermarked_outputfile.pdf', 'wb')
pdfWriter.write(resultPdfFile)
originalFile.close()
resultPdfFile.close()

Recent Posts

  • Learn Kotlin – Class
  • Learn Kotlin – Function
  • Learn Kotlin – Control Flow
  • Learn Kotlin – Array, Collection and Range
  • Learn Kotlin – Hello world and Basic Data Types

Categories

AndEngine Android Android App C++ C Sharp Django Functional Programming Game Game Development Github Haskell JavaScript Kotlin Laravel PHP Python Ubuntu Uncategorized Unity3D Windows 8 WordPress

Archives

  • January 2019
  • December 2018
  • December 2017
  • November 2017
  • July 2017
  • May 2017
  • March 2017
  • November 2016
  • October 2015
  • September 2015
  • April 2015
  • January 2015
  • September 2014
  • August 2014
  • June 2014
  • May 2014
  • April 2014
  • February 2014
  • January 2014
  • December 2013

Follow me on Twitter

My Tweets
असतोमा सद्गमय ।
तमसोमा ज्योतिर् गमय ।
मृत्योर्मामृतं गमय ॥
ॐ शान्ति शान्ति शान्तिः ।।

Powered by WordPress.com.

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy