LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-22-2014, 11:38 AM   #1
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Rep: Reputation: 47
command line to convert png to pdf


I was using phantomjs to make PDFs of a webpage on a regular basis, but not it only works if I have it generate PNGs rather than PDFs.
So is there some secondary command I can run to look in a folder and convert all the PNGs in there to PDFs, then remove the PNGs?
 
Old 09-22-2014, 12:24 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Imagemagik
Code:
convert file.png file.pdf
 
1 members found this post helpful.
Old 09-22-2014, 12:34 PM   #3
yancek
LQ Guru
 
Registered: Apr 2008
Distribution: PCLinux, Slackware
Posts: 6,978

Rep: Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284Reputation: 1284
Use ImageMagick which is installed on most Linux systems by default. Simply using the convert command should work. convert file.png file.pdf as an example. If you want to do multiple png files to pdf in the same directory you can just modify the command to suit your needs and whether you want the .png files created to one .pdf or multiple. You can find examples for different situations at the link below.

http://ubuntuforums.org/showthread.php?t=789359
 
1 members found this post helpful.
Old 09-22-2014, 12:34 PM   #4
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
So could I just simply do a convert /my/directory/*.png /my/directory/*.pdf to script it so it'll do all the files always?
 
Old 09-22-2014, 12:36 PM   #5
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
looks like I was writing while you were posting yancek. So sounds like my command would work then, making it a 1:1 ratio for png to pdf.
 
Old 09-22-2014, 01:55 PM   #6
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
If you don't care of png files backup
Code:
mogrify -format pdf /my/directory/*.png
 
1 members found this post helpful.
Old 09-22-2014, 02:01 PM   #7
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Sid, Linux Mint DE
Posts: 4,493

Rep: Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636Reputation: 636
Depending on the image contents jpg format might be considerably smaller than png. The resulting pdf is about the same size as the source image. If file size is a concern, consider converting png to jpg first, then convert jpg to pdf. It is the same Imagemagick command, but different extensions.

jlinkels
 
1 members found this post helpful.
Old 09-22-2014, 04:17 PM   #8
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
I don't necessarily care about the PNGs, as they want PDFs, but maybe I should have it keep them for a while until I know the PDFs are good. But sounds like when I know i'm good to not keep the PNGs as a backup, mogrify might be better suited.

That's a good tip to save space, thanks.
 
Old 09-22-2014, 05:40 PM   #9
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
if I want to add the convert into a .sh script, but only want it to convert and delete the most recent (like if I did a ll -rt, the last files in those results), how could I do that?

I figure i'll leave all the existing PNGs as is, but i'd like it to start converting the new ones it generates into PDF while deleting the PNG. that way those PNGs will eventually work themselves out when they purge after a year.
 
Old 09-22-2014, 05:48 PM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 5,388

Rep: Reputation: 398Reputation: 398Reputation: 398Reputation: 398
Say for 10 first files listed with ls -rt
Code:
for file in $(ls -rt *.png | head -10); do
   convert $file $file.pdf
done
resulting filenames will be someting like file.png.pdf
maybe adjust the script to give some better names

Edit,
with -r option (reverse), you list the oldest files first, not the newest

Last edited by keefaz; 09-22-2014 at 05:53 PM.
 
1 members found this post helpful.
Old 09-22-2014, 07:51 PM   #11
rjo98
Senior Member
 
Registered: Jun 2009
Location: US
Distribution: RHEL, CentOS
Posts: 1,674

Original Poster
Rep: Reputation: 47
Thanks for the help everybody, I think I'm finally set. Wish I knew why the PDFs stopped working, but this workaround at least puts things back to "normal" for now to the users.
 
Old 10-19-2014, 07:06 PM   #12
OpensourceRulzs
LQ Newbie
 
Registered: Oct 2014
Posts: 14

Rep: Reputation: Disabled
I use gnome-web-photo to convert a webpage straight to pdf. No need for images to download and convert.

Code:
gnome-web-photo -t 0 --mode=print http://example.com output.pdf

Last edited by OpensourceRulzs; 10-19-2014 at 07:12 PM.
 
Old 10-20-2014, 07:17 PM   #13
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,307

Rep: Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566Reputation: 566
Quote:
Originally Posted by jlinkels View Post
Depending on the image contents jpg format might be considerably smaller than png. The resulting pdf is about the same size as the source image. If file size is a concern, consider converting png to jpg first, then convert jpg to pdf. It is the same Imagemagick command, but different extensions.

jlinkels
Just remember that jpegs don't support transparency, if you need transpency use gifs instead of jpegs, file size is not as small as jpegs but smaller than pngs
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
cmmand line to convert pdf to text ufmale Linux - Software 1 09-13-2008 01:48 AM
[SOLVED] command line script to change resolution of png files kaz2100 Programming 3 06-04-2007 08:53 PM
PDF to Command line format Tux-O-Matic General 6 10-04-2006 08:05 AM
convert doc to pdf in command line yumener Linux - Software 4 04-28-2006 09:23 AM
CMD line tool for PDF -> HTML or JPG/PNG/GIF ilhbutshm Linux - Software 5 10-23-2004 05:18 AM


All times are GMT -5. The time now is 07:14 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration