diff --git a/readme.txt b/readme.txt index 4ce7ed4..a3f03e2 100644 --- a/readme.txt +++ b/readme.txt @@ -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. + + + diff --git a/src/clipdfx b/src/clipdfx deleted file mode 100755 index e722bc6..0000000 --- a/src/clipdfx +++ /dev/null @@ -1,73 +0,0 @@ -#!/bin/bash -# -# Copyright (C) 2011 Christoph Lehner, Nathan Goldschmidt -# -# v1[Lehner]: Original version -# v2[Goldschmidt]: Use ghostscript to remove invisible content, format FN -# v3[Lehner]: Streamline code -# -# All programs in this directory and subdirectories are published under the GNU -# General Public License as described below. -# -# This program is free software; you can redistribute it and/or modify it under -# the terms of the GNU General Public License as published by the Free Software -# Foundation; either version 2 of the License, or (at your option) any later -# version. -# -# This program is distributed in the hope that it will be useful, but WITHOUT -# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS -# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more -# details. -# -# You should have received a copy of the GNU General Public License along with -# this program; if not, write to the Free Software Foundation, Inc., 59 Temple -# Place, Suite 330, Boston, MA 02111-1307 USA -# -# Further information about the GNU GPL is available at: -# http://www.gnu.org/copyleft/gpl.ja.html - -# -# Assign arguments -# -fn=$1 -fnout=$2 -page=$3 -left=$4 -top=$5 -right=$6 -bottom=$7 - -# -# Create temporary directory -# -tdir=$(mktemp -d) -mkdir -p $tdir - -# -# Use a simple filename for LaTeX with .pdf extension -# -cp -f "$fn" $tdir/i.pdf - -# -# Use LaTeX to extract the -# -cat > $tdir/o.tex < + +# 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) @@ -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') @@ -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/%') @@ -107,7 +147,7 @@ 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) @@ -115,41 +155,41 @@ class Poprender(object): 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() @@ -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) @@ -178,7 +218,7 @@ 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) @@ -186,7 +226,7 @@ class Poprender(object): 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 @@ -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: