[ocrfeeder] Add PDF exportation
- From: Joaquim Manuel Pereira Rocha <jrocha src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [ocrfeeder] Add PDF exportation
- Date: Tue, 2 Aug 2011 21:51:39 +0000 (UTC)
commit d73ebc447451da8816a80ad14e316ab9d6cdda06
Author: Joaquim Rocha <jrocha igalia com>
Date: Wed Jul 27 15:33:01 2011 +0200
Add PDF exportation
Exports to a PDF from scratch. Might not respect some of
configurations like the fonts and letter spacing which should be
fine-tuned in the future.
src/ocrfeeder/feeder/documentGeneration.py | 47 ++++++++++++++++++++++++++++
src/ocrfeeder/studio/studioBuilder.py | 6 +++-
src/ocrfeeder/studio/widgetModeler.py | 14 ++++++++-
3 files changed, 65 insertions(+), 2 deletions(-)
---
diff --git a/src/ocrfeeder/feeder/documentGeneration.py b/src/ocrfeeder/feeder/documentGeneration.py
index ba218aa..3b2b2be 100644
--- a/src/ocrfeeder/feeder/documentGeneration.py
+++ b/src/ocrfeeder/feeder/documentGeneration.py
@@ -29,6 +29,9 @@ from ocrfeeder.util import TEXT_TYPE, IMAGE_TYPE, ALIGN_LEFT, ALIGN_RIGHT, ALIGN
ALIGN_FILL
from ocrfeeder.util.graphics import getImagePrintSize
from ocrfeeder.util.lib import debug
+from reportlab.pdfgen import canvas
+from reportlab.lib import units
+from reportlab.lib.utils import ImageReader
import math
import os.path
import shutil
@@ -349,3 +352,47 @@ class PlaintextGenerator(DocumentGenerator):
f.close() # Close the file
except IOError:
pass
+
+class PdfGenerator(DocumentGenerator):
+
+ def __init__(self, name):
+ self.name = name
+ self.canvas = canvas.Canvas(self.name)
+ self.page_data = None
+
+ def addText(self, box):
+ x, y, width, height = box.getBoundsPrintSize(self.page_data.resolution)
+ text = self.canvas.beginText()
+ text.setTextOrigin(x * units.inch,
+ (self.page_data.height - y) * units.inch)
+ text.setCharSpace(box.text_data.letter_space)
+ text.setLeading(box.text_data.line_space + box.text_data.size)
+ text.moveCursor(0, box.text_data.size)
+ try:
+ self.setFont(box.text_data.face,
+ box.text_data.size)
+ except:
+ debug('Error setting font %s' % box.text_data.face)
+ self.canvas.setFontSize(box.text_data.size)
+ for line in box.text.split('\n'):
+ text.textLine(line)
+ self.canvas.drawText(text)
+
+ def addImage(self, box):
+ x, y, width, height = box.getBoundsPrintSize(self.page_data.resolution)
+ self.canvas.drawInlineImage(box.image,
+ x * units.inch,
+ (self.page_data.height - (y + height)) * \
+ units.inch,
+ width * units.inch,
+ height * units.inch)
+
+ def addPage(self, page_data):
+ self.canvas.setPageSize((page_data.width * units.inch,
+ page_data.height * units.inch))
+ self.page_data = page_data
+ self.addBoxes(page_data.data_boxes)
+ self.canvas.showPage()
+
+ def save(self):
+ self.canvas.save()
diff --git a/src/ocrfeeder/studio/studioBuilder.py b/src/ocrfeeder/studio/studioBuilder.py
index 0f0599e..33d34c2 100644
--- a/src/ocrfeeder/studio/studioBuilder.py
+++ b/src/ocrfeeder/studio/studioBuilder.py
@@ -60,7 +60,8 @@ class Studio:
self.EXPORT_FORMATS = {0: (self.exportToHtml, _('HTML')),
1: (self.exportToOdt, _('ODT')),
- 2: (self.exportToPlaintext, _('Plain Text'))}
+ 2: (self.exportToPdf, _('PDF')),
+ 3: (self.exportToPlaintext, _('Plain Text'))}
self.title = OCRFEEDER_STUDIO_NAME
self.main_window = widgetPresenter.MainWindow()
@@ -290,6 +291,9 @@ class Studio:
def exportToPlaintext(self, widget = None):
self.source_images_controler.exportPagesToPlaintext(self.source_images_selector.getPixbufsSorted())
+ def exportToPdf(self, widget = None):
+ self.source_images_controler.exportPagesToPdf(self.source_images_selector.getPixbufsSorted())
+
def exportDialog(self, widget):
format_names = [format[1] for format in self.EXPORT_FORMATS.values()]
export_dialog = widgetPresenter.ExportDialog(_('Export pages'), format_names)
diff --git a/src/ocrfeeder/studio/widgetModeler.py b/src/ocrfeeder/studio/widgetModeler.py
index 1ec4b69..0adb850 100644
--- a/src/ocrfeeder/studio/widgetModeler.py
+++ b/src/ocrfeeder/studio/widgetModeler.py
@@ -20,7 +20,7 @@
from customWidgets import SelectableBoxesArea
from dataHolder import DataBox, PageData, TEXT_TYPE, IMAGE_TYPE
-from ocrfeeder.feeder.documentGeneration import OdtGenerator, HtmlGenerator, PlaintextGenerator
+from ocrfeeder.feeder.documentGeneration import OdtGenerator, HtmlGenerator, PlaintextGenerator, PdfGenerator
from ocrfeeder.feeder.imageManipulation import *
from ocrfeeder.feeder.layoutAnalysis import *
from pango import FontDescription, SCALE
@@ -709,6 +709,18 @@ class ImageReviewer_Controler:
document_generator.addText(image_reviewer.getAllText())
document_generator.save()
+ def exportPagesToPdf(self, pixbufs_sorted = []):
+ image_reviewers = self.__askForNumberOfPages(_('Export to PDF'),
+ pixbufs_sorted)
+ if not image_reviewers:
+ return
+ file_name = self.__askForFileName()
+ if file_name:
+ document_generator = PdfGenerator(file_name)
+ for image_reviewer in image_reviewers:
+ document_generator.addPage(image_reviewer.getPageData())
+ document_generator.save()
+
def saveProjectAs(self):
return self.__askForFileName(extension = '.ocrf')
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]