Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace the shell script with a python method #1

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,17 @@ gpdfx is a GTK application written in Python using the Poppler library
to render the PDF. It uses pdfTeX and PDFCrop to create the extracted
PDF.

Author
* Christoph Lehner (clehner // users.sourceforge.net)
Original Author

* Christoph Lehner (clehner // users.sourceforge.net)

Changes from origin:

Replace the shell script with a Python method so that the
whole GUI and Converter are contained in one file.

These changed ease the installation and usage of this little useful tool
a little bit.



73 changes: 0 additions & 73 deletions src/clipdfx

This file was deleted.

149 changes: 92 additions & 57 deletions src/gpdfx
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,63 @@
#
# Further information about the GNU GPL is available at:
# http://www.gnu.org/copyleft/gpl.ja.html

# Copyright (C) 2013 Oz Nahum <nahumoz@gmail.com>

# The Original gpdfx was obtained from https://github.com/lehner/gpdfx.
# The changed done are mainly to replace the shell script with a Python
# method so that the whole GUI and Converter are contained in on file.

# These changed ease the installation and usage of this little useful tool
# a little bit

import pygtk
pygtk.require('2.0')
import gtk
import poppler
import sys, os
import cairo
import sys
import os
import subprocess as sp
import tempfile
import shutil


def ConverPartPDF(fname, fout, page, left, top, right, bottom):
tdir = tempfile.mkdtemp()
shutil.copy(fname, os.path.join(tdir, 'i.pdf'))

lines = """
\\documentclass{{article}}
\\usepackage[left=0cm,right=0cm,bottom=0cm,top=0cm]{{geometry}}
\\usepackage{{graphicx}}
\\begin{{document}}
\\includegraphics[page={0},trim={1} {2} {3} {4},clip]{{{5}/i.pdf}}
\\end{{document}}
""".format(page, left, bottom, right, top, tdir)

with open(os.path.join(tdir, 'o.tex'), 'w') as output:
output.write(lines)

sp.call('pdflatex o.tex', shell=True, cwd=tdir)
sp.call('pdfcrop o.pdf', shell=True, cwd=tdir)
sp.call(['gs', '-q', '-sDEVICE=pdfwrite', '-dNOPAUSE',
'-dBATCH', '-sOutputFile='+fout, '-f',
os.path.join(tdir, 'o-crop.pdf')],
cwd=tdir)

shutil.rmtree(tdir)


class Poprender(object):

def __init__(self):
self.fn = os.path.expanduser(sys.argv[1])
self.fn = os.path.expandvars(self.fn)
self.fn = os.path.abspath(self.fn)
uri = "file://" + self.fn

try:
self.document = poppler.document_new_from_file (uri, None)
self.document = poppler.document_new_from_file(uri, None)
except:
sys.exit(1)

Expand All @@ -60,7 +100,7 @@ class Poprender(object):
self.win.connect("delete-event", gtk.main_quit)

adjust = gtk.Adjustment(1, 1, self.n_pages, 1, 5)
page_selector = gtk.SpinButton(adjust, 0, 0);
page_selector = gtk.SpinButton(adjust, 0, 0)
page_selector.connect("value-changed", self.on_changed)

lab = gtk.Label('Page')
Expand All @@ -74,7 +114,7 @@ class Poprender(object):
hbox.pack_start(page_selector, False, False, 0)

adjust = gtk.Adjustment(100, 25, 400, 25, 100)
scale_selector = gtk.SpinButton(adjust, 0, 0);
scale_selector = gtk.SpinButton(adjust, 0, 0)
scale_selector.connect("value-changed", self.on_scale_changed)

lab = gtk.Label('Zoom/%')
Expand Down Expand Up @@ -107,49 +147,49 @@ class Poprender(object):

self.sw.add_with_viewport(eventbox)
self.dwg.modify_bg(gtk.STATE_NORMAL,
gtk.gdk.Color(50000,50000,50000))
gtk.gdk.Color(50000, 50000, 50000))

vbox.pack_start(self.sw, True, True, 0)

self.win.add(vbox)
self.win.show_all()

def sel_pos_clip(self):
if self.sel_x>self.width:
self.sel_x=self.width
if self.sel2_x>self.width:
self.sel2_x=self.width
if self.sel_y>self.height:
self.sel_y=self.height
if self.sel2_y>self.height:
self.sel2_y=self.height

if self.sel_x<0:
self.sel_x=0
if self.sel2_x<0:
self.sel2_x=0
if self.sel_y<0:
self.sel_y=0
if self.sel2_y<0:
self.sel2_y=0
if self.sel_x > self.width:
self.sel_x = self.width
if self.sel2_x > self.width:
self.sel2_x = self.width
if self.sel_y > self.height:
self.sel_y = self.height
if self.sel2_y > self.height:
self.sel2_y = self.height

if self.sel_x < 0:
self.sel_x = 0
if self.sel2_x < 0:
self.sel2_x = 0
if self.sel_y < 0:
self.sel_y = 0
if self.sel2_y < 0:
self.sel2_y = 0

def on_btn_down(self, widget, event):
self.sel_x=event.x/self.scale
self.sel_y=event.y/self.scale
self.sel2_x=event.x/self.scale
self.sel2_y=event.y/self.scale
self.sel_x = event.x/self.scale
self.sel_y = event.y/self.scale
self.sel2_x = event.x/self.scale
self.sel2_y = event.y/self.scale
self.sel_pos_clip()
self.sel = True

def on_btn_up(self, widget, event):
self.sel2_x=event.x/self.scale
self.sel2_y=event.y/self.scale
self.sel2_x = event.x/self.scale
self.sel2_y = event.y/self.scale
self.sel_pos_clip()
self.dwg.queue_draw()

def on_mouse_move(self, widget, event):
self.sel2_x=event.x/self.scale
self.sel2_y=event.y/self.scale
self.sel2_x = event.x/self.scale
self.sel2_y = event.y/self.scale
self.sel_pos_clip()
self.dwg.queue_draw()

Expand All @@ -167,7 +207,7 @@ class Poprender(object):
int(self.height*self.scale))
self.dwg.queue_draw()

def cr_draw(self,cr,width,height,scale):
def cr_draw(self, cr, width, height, scale):
if scale != 1:
cr.scale(scale, scale)
cr.set_source_rgb(1, 1, 1)
Expand All @@ -178,15 +218,15 @@ class Poprender(object):
if self.sel:
cr.set_source_rgba(0, 0, 0.5, 0.9)
cr.set_line_width(1)
cr.rectangle(self.sel_x,self.sel_y,self.sel2_x-self.sel_x,
cr.rectangle(self.sel_x, self.sel_y, self.sel2_x-self.sel_x,
self.sel2_y-self.sel_y)
cr.stroke_preserve()
cr.set_source_rgba(0, 0, 1, 0.2)
cr.fill()

def on_expose(self, widget, event):
cr = widget.window.cairo_create()
self.cr_draw(cr,self.width,self.height,self.scale)
self.cr_draw(cr, self.width, self.height, self.scale)

def on_clear_sel(self, widget):
self.sel = False
Expand All @@ -195,44 +235,39 @@ class Poprender(object):
def on_export(self, widget):
if self.sel:

if self.sel_x>self.sel2_x:
tmp=self.sel_x
self.sel_x=self.sel2_x
self.sel2_x=tmp
if self.sel_x > self.sel2_x:
tmp = self.sel_x
self.sel_x = self.sel2_x
self.sel2_x = tmp

if self.sel_y>self.sel2_y:
tmp=self.sel_y
self.sel_y=self.sel2_y
self.sel2_y=tmp
if self.sel_y > self.sel2_y:
tmp = self.sel_y
self.sel_y = self.sel2_y
self.sel2_y = tmp

ch = gtk.FileChooserDialog("Filename for selection PDF",None,
ch = gtk.FileChooserDialog("Filename for selection PDF", None,
gtk.FILE_CHOOSER_ACTION_SAVE,
(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN,gtk.RESPONSE_OK))
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OPEN, gtk.RESPONSE_OK))
ch.set_do_overwrite_confirmation(True)
fnout = None
if ch.run() == gtk.RESPONSE_OK:
fnout = ch.get_filename()
ch.destroy()

if fnout!=None:
exportPdf(self.fn,fnout,self.n_page,
self.sel_x,self.sel_y,
self.width-self.sel2_x,
self.height-self.sel2_y)
if fnout is not None:
ConverPartPDF(self.fn, fnout, self.n_page,
self.sel_x, self.sel_y,
self.width-self.sel2_x,
self.height-self.sel2_y)

def main(self):
gtk.main()


def exportPdf(fn,fnout,npage,left,top,right,bottom):
os.system("clipdfx \"" + fn + "\" \"" + fnout + "\" " + str(npage) + " "
+ str(left) + " " + str(top) + " " + str(right) + " "
+ str(bottom))

if __name__ == '__main__':
try:
if len(sys.argv)==2:
if len(sys.argv) == 2:
pop = Poprender()
pop.main()
else:
Expand Down