Commit 80ccaf3b authored by Fabien Morin's avatar Fabien Morin

- change the height and width order to have everywhere the same order (same as

  the
  output command order)
- remove the use of pdftoppm. So now only convert and pdfinfo are used, so now
  xpdf
  installation is not required to use erp5_pdf_editor bt
- add verbose parameter on convert command. This permit to get background real
  size using a unique command.
- regular expressions are now used to parse command output, this is more
  efficient than all split() and easyer to maintain
- add a comment on getPageattribute method
- simplification of getPageattribute method : remove the use of pdftoppm, just
  return one width and one height because there is no sens to return all page
  width and height when convert will set all pages to the same size.
- rename getPageattribute method to respect naming convention



git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@20309 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent 55c25bb6
...@@ -197,15 +197,15 @@ def ERP5Site_createModuleScribus(self, ...@@ -197,15 +197,15 @@ def ERP5Site_createModuleScribus(self,
# used only with option_html == 1 # used only with option_html == 1
# recover image_size # recover image_size
image_size = ManageFiles.setBackgroundPictures(import_pdf_file, image_size = ManageFiles.setBackgroundPictures(import_pdf_file,
object_names, skin_folder, desired_height, desired_width, object_names, skin_folder, desired_width, desired_height,
resolution, background_format) resolution, background_format)
page_width, page_height = image_size new_width, new_height = image_size
LOG('ERP5Site_createModuleScribus', INFO, LOG('ERP5Site_createModuleScribus', INFO,
' height = %s' % str(page_height)) ' height = %s' % str(new_height))
LOG('ERP5Site_createModuleScribus', INFO, LOG('ERP5Site_createModuleScribus', INFO,
' width = %s' % str(page_width)) ' width = %s' % str(new_width))
LOG('ERP5Site_createModuleScribus', INFO, LOG('ERP5Site_createModuleScribus', INFO,
'createmodule < background generated') 'createmodule < background generated')
...@@ -239,17 +239,17 @@ def ERP5Site_createModuleScribus(self, ...@@ -239,17 +239,17 @@ def ERP5Site_createModuleScribus(self,
# get CSS class properties relative to the actual page # get CSS class properties relative to the actual page
# (background picture position, picture size, etc.) # (background picture position, picture size, etc.)
# and add them to the css dict # and add them to the css dict
width_groups, height_groups = ManageFiles.getPageattributes( old_width, old_height = ManageFiles.getPageAttributes(
global_properties, global_properties,
import_pdf_file) import_pdf_file)
properties_css_dict, properties_page = \ properties_css_dict, properties_page = \
ManageCSS.setPageProperties(properties_css_dict, ManageCSS.setPageProperties(properties_css_dict,
page_iterator, page_iterator,
page_id, page_id,
page_height, new_width,
page_width, new_height,
width_groups, old_width,
height_groups) old_height)
# RESUME DATA INTERPRETATION # RESUME DATA INTERPRETATION
# iterating pageobjects in page # iterating pageobjects in page
...@@ -266,8 +266,8 @@ def ERP5Site_createModuleScribus(self, ...@@ -266,8 +266,8 @@ def ERP5Site_createModuleScribus(self,
properties_css_dict = ManageCSS.setFieldProperties( properties_css_dict = ManageCSS.setFieldProperties(
properties_css_dict, properties_css_dict,
page_content[index], page_content[index],
page_width, new_width,
page_height, new_height,
page_iterator, page_iterator,
page_gap, page_gap,
keep_page, keep_page,
...@@ -289,7 +289,7 @@ def ERP5Site_createModuleScribus(self, ...@@ -289,7 +289,7 @@ def ERP5Site_createModuleScribus(self,
# add last properties to css dict, including implementation # add last properties to css dict, including implementation
# of a n+1 page to prevent bug when rendering under Konqueror # of a n+1 page to prevent bug when rendering under Konqueror
ManageCSS.setFinalProperties(properties_css_dict, page_height, ManageCSS.setFinalProperties(properties_css_dict, new_height,
space_between_pages) space_between_pages)
# generate output string from dict # generate output string from dict
......
...@@ -221,15 +221,15 @@ def ERP5Site_updateModuleScribus(self, ...@@ -221,15 +221,15 @@ def ERP5Site_updateModuleScribus(self,
# used only with option_html == 1 # used only with option_html == 1
# recover image_size # recover image_size
image_size = ManageFiles.setBackgroundPictures(import_pdf_file, image_size = ManageFiles.setBackgroundPictures(import_pdf_file,
object_names, skin_folder, desired_height, desired_width, object_names, skin_folder, desired_width, desired_height,
resolution, background_format) resolution, background_format)
page_width, page_height = image_size new_width, new_height = image_size
LOG('ERP5Site_updateModuleScribus', INFO, LOG('ERP5Site_updateModuleScribus', INFO,
' height = %s' % str(page_height)) ' height = %s' % str(new_height))
LOG('ERP5Site_updateModuleScribus', INFO, LOG('ERP5Site_updateModuleScribus', INFO,
' width = %s' % str(page_width)) ' width = %s' % str(new_width))
LOG('ERP5Site_updateModuleScribus', INFO, LOG('ERP5Site_updateModuleScribus', INFO,
'createmodule < background generated') 'createmodule < background generated')
...@@ -263,17 +263,17 @@ def ERP5Site_updateModuleScribus(self, ...@@ -263,17 +263,17 @@ def ERP5Site_updateModuleScribus(self,
# get CSS class properties relative to the actual page # get CSS class properties relative to the actual page
# (background picture position, picture size, etc.) # (background picture position, picture size, etc.)
# and add them to the css dict # and add them to the css dict
width_groups, height_groups = ManageFiles.getPageattributes( old_width, old_height = ManageFiles.getPageAttributes(
global_properties, global_properties,
import_pdf_file) import_pdf_file)
properties_css_dict, properties_page = \ properties_css_dict, properties_page = \
ManageCSS.setPageProperties(properties_css_dict, ManageCSS.setPageProperties(properties_css_dict,
page_iterator, page_iterator,
page_id, page_id,
page_height, new_width,
page_width, new_height,
width_groups, old_width,
height_groups) old_height)
# RESUME DATA INTERPRETATION # RESUME DATA INTERPRETATION
# iterating pageobjects in page # iterating pageobjects in page
...@@ -290,8 +290,8 @@ def ERP5Site_updateModuleScribus(self, ...@@ -290,8 +290,8 @@ def ERP5Site_updateModuleScribus(self,
properties_css_dict = ManageCSS.setFieldProperties( properties_css_dict = ManageCSS.setFieldProperties(
properties_css_dict, properties_css_dict,
page_content[index], page_content[index],
page_width, new_width,
page_height, new_height,
page_iterator, page_iterator,
page_gap, page_gap,
keep_page, keep_page,
...@@ -313,7 +313,7 @@ def ERP5Site_updateModuleScribus(self, ...@@ -313,7 +313,7 @@ def ERP5Site_updateModuleScribus(self,
# add last properties to css dict, including implementation # add last properties to css dict, including implementation
# of a n+1 page to prevent bug when rendering under Konqueror # of a n+1 page to prevent bug when rendering under Konqueror
ManageCSS.setFinalProperties(properties_css_dict, page_height, ManageCSS.setFinalProperties(properties_css_dict, new_height,
space_between_pages) space_between_pages)
# generate output string from dict # generate output string from dict
......
...@@ -38,7 +38,7 @@ from StringIO import StringIO ...@@ -38,7 +38,7 @@ from StringIO import StringIO
from zLOG import LOG, TRACE, WARNING, ERROR, INFO from zLOG import LOG, TRACE, WARNING, ERROR, INFO
import imghdr import imghdr
import random import random
import getopt, sys, os import getopt, sys, os, re
from urllib import quote from urllib import quote
from Products.ERP5.ERP5Site import ERP5Site from Products.ERP5.ERP5Site import ERP5Site
...@@ -568,8 +568,8 @@ class ManageFiles: ...@@ -568,8 +568,8 @@ class ManageFiles:
pdf_file, pdf_file,
object_names, object_names,
skin_folder, skin_folder,
desired_height,
desired_width, desired_width,
desired_height,
resolution, resolution,
background_format background_format
): ):
...@@ -588,8 +588,10 @@ class ManageFiles: ...@@ -588,8 +588,10 @@ class ManageFiles:
# opening new file on HDD to save PDF content # opening new file on HDD to save PDF content
temp_pdf = NamedTemporaryFile() temp_pdf = NamedTemporaryFile()
# opening new file on HDD to save created background image content # get an unused file name on HDD to save created background image content
temp_image = NamedTemporaryFile() temp_image = NamedTemporaryFile()
temp_image_name = temp_image.name
temp_image.close()
# going to the begining of the input file # going to the begining of the input file
pdf_file.seek(0) pdf_file.seek(0)
...@@ -602,21 +604,21 @@ class ManageFiles: ...@@ -602,21 +604,21 @@ class ManageFiles:
# convert add a '-N' string a the end of the file name if there is more # convert add a '-N' string a the end of the file name if there is more
# than one page in the pdf file (where N is the number of the page, # than one page in the pdf file (where N is the number of the page,
# begining at 0) # begining at 0)
if os.path.exists(temp_image.name): if os.path.exists(temp_image_name):
# thats mean there's only one page in the pdf file # thats mean there's only one page in the pdf file
background_image_list.append(temp_image.name) background_image_list.append(temp_image_name)
else: else:
# in the case of multi-pages pdf file, we must find all files # in the case of multi-pages pdf file, we must find all files
image_number = 0 image_number = 0
while os.path.exists(temp_image.name + '-%s' % image_number): while os.path.exists(temp_image_name + '-%s' % image_number):
background_image_list.append(temp_image.name + '-%s' % image_number) background_image_list.append(temp_image_name + '-%s' % image_number)
image_number += 1 image_number += 1
return background_image_list return background_image_list
try: try:
result = commands.getstatusoutput('convert -density %s -resize %sx%s '\ result = commands.getstatusoutput('convert -verbose -density %s -resize %sx%s '\
'%s %s' % (resolution, desired_width, desired_height, temp_pdf.name, '%s %s' % (resolution, desired_width, desired_height, temp_pdf.name,
background_format + ':' + temp_image.name)) background_format + ':' + temp_image_name))
# check that the command has been done succeful # check that the command has been done succeful
if result[0] != 0: if result[0] != 0:
...@@ -638,18 +640,26 @@ class ManageFiles: ...@@ -638,18 +640,26 @@ class ManageFiles:
if not len(background_image_list): if not len(background_image_list):
LOG('ScribusUtils.setBackgroundPictures :', ERROR, 'no background '\ LOG('ScribusUtils.setBackgroundPictures :', ERROR, 'no background '\
'image found') 'image found')
temp_image.close()
raise ValueError, 'Error: ScribusUtils.setBackgroundPictures : '\ raise ValueError, 'Error: ScribusUtils.setBackgroundPictures : '\
'no background' 'no background'
# get the real size of the first image # get the real size of the first image
# this could be usefull if the user only defined one dimention # this could be usefull if the user only defined one dimention :
# and convert has calculate the other using proportionnality # convert command has calculate the other using proportionnality
file_result= commands.getstatusoutput('identify %s' %\ rawstr = r'''
background_image_list[0]) PDF\s # The line begin with PDF
(\d+)x(\d+) # old file size
real_size_x = file_result[1].split(' ')[2].split('x')[0] => # Separator between old and new size
real_size_y = file_result[1].split(' ')[2].split('x')[1] (\d+)x(\d+) # The matching pattern : width and height'''
compile_obj = re.compile(rawstr, re.MULTILINE | re.VERBOSE)
match_obj_list = re.findall(compile_obj, result[1])
# old size is not use now, and depends of the density convert parameter
#old_size_x = match_obj_list[0][0]
#old_size_y = match_obj_list[0][1]
real_size_x = match_obj_list[0][2]
real_size_y = match_obj_list[0][3]
# add images in erp5 : # add images in erp5 :
image_number = 0 image_number = 0
...@@ -662,11 +672,10 @@ class ManageFiles: ...@@ -662,11 +672,10 @@ class ManageFiles:
# delete all images created in this method before raise an error # delete all images created in this method before raise an error
background_image_list = makeImageList() background_image_list = makeImageList()
for background_image in background_image_list[1:]: for background_image in background_image_list:
# remove the file from the system # remove the file from the system
if os.path.exists(background_image): if os.path.exists(background_image):
os.remove(background_image) os.remove(background_image)
temp_image.close()
size_x = int(real_size_x) size_x = int(real_size_x)
size_y = int(real_size_y) size_y = int(real_size_y)
...@@ -675,11 +684,14 @@ class ManageFiles: ...@@ -675,11 +684,14 @@ class ManageFiles:
return (size_x, size_y) return (size_x, size_y)
security.declarePublic('getPageattributes') security.declarePublic('getPageAttributes')
def getPageattributes (self, def getPageAttributes (self,
global_properties, global_properties,
pdf_file pdf_file
): ):
'''return the size of the original pdf file (in pts)
This is usefull to calculate proportionnality between original size and
desired size. That permit to place correctly the fields on the page'''
import commands import commands
from tempfile import NamedTemporaryFile from tempfile import NamedTemporaryFile
# opening new file on HDD to save PDF content # opening new file on HDD to save PDF content
...@@ -694,61 +706,32 @@ class ManageFiles: ...@@ -694,61 +706,32 @@ class ManageFiles:
pdf_file.seek(0) pdf_file.seek(0)
# saving content # saving content
temp_pdf.write(pdf_file.read()) temp_pdf.write(pdf_file.read())
if os.path.exists(temp_pdf.name):
temp_pdf.close() temp_pdf.close()
width_groups = []
height_groups = []
# launching first soft to convert from PDF to PPM
ScribusUtilsOriginaltempsPPM = NamedTemporaryFile(mode="w+b")
ScribusUtilsOriginaltempsPPMName = ScribusUtilsOriginaltempsPPM.name
original_result = commands.getstatusoutput('pdftoppm -r %s %s %s' % (72,
ScribusUtilsOriginaltempsPDFName, ScribusUtilsOriginaltempsPPMName))
original_result= commands.getstatusoutput('identify %s' % \
(ScribusUtilsOriginaltempsPPMName + '*'))
def makePPMFileList():
ppm_list = []
# pdftoppm add a '-N' string a the end of the file name if there is more
# than one page in the pdf file (where N is the number of the page,
# begining at 1)
if os.path.exists(ScribusUtilsOriginaltempsPPMName):
# thats mean there's only one page in the pdf file
ppm_list.append(ScribusUtilsOriginaltempsPPMName)
# try to find the other pages if there is more than one (in case of
# multi-pages pdf file)
image_number = 1
while os.path.exists(ScribusUtilsOriginaltempsPPMName + '-%s.ppm' %\
image_number):
ppm_list.append(ScribusUtilsOriginaltempsPPMName + '-%s.ppm' % \
image_number)
image_number += 1
return ppm_list
# this lines permit to delete tempory files (about 2.4 Mo for each file !)
# it's temporary because this function must be rewrited or deleted
# (perhaps setBackgroundPictures could return attributes list)
ppm_list = makePPMFileList()
for ppm in ppm_list[1:]:
if os.path.exists(ppm):
os.remove(ppm)
ScribusUtilsOriginaltempsPPM.close()
ScribusUtilsOriginalTempPDF.close()
pg_nbr = len(original_result[1].split('\n'))
real_size_x = {}
real_size_y = {}
for i in range(0,pg_nbr):
real_size_x[i]= \
float(original_result[1].split('\n')[i].split(' ')[2].split('x')[1])
real_size_y[i]= \
float(original_result[1].split('\n')[i].split(' ')[2].split('x')[0])
for page_iterator in range(global_properties['page']):
actual_page_height = real_size_x[page_iterator]
actual_page_width = real_size_y[page_iterator]
width_groups.append(actual_page_width)
height_groups.append(actual_page_height)
return (width_groups, height_groups)
command_output = commands.getstatusoutput('pdfinfo %s' % \
ScribusUtilsOriginaltempsPDFName)
if command_output[0] != 0:
LOG('ScribusUtils.getPageAttributes :', ERROR, 'pdfinfo command'\
'failed with the following error message : \n%s' % command_output[1])
raise ValueError, 'Error: convert command failed with the following'\
'error message : \n%s' % command_output[1]
# get the pdf page size
rawstr = r'''
Page\ssize: #begining of the instersting line
\s* #some spaces
(\S+)\sx\s(\S+) #the matching pattern : width and height in pts'''
compile_obj = re.compile(rawstr, re.MULTILINE | re.VERBOSE)
match_obj = re.search(compile_obj, command_output[1])
width, height = match_obj.groups()
width = int(round(float(width)))
height = int(round(float(height)))
LOG('width_groups, height_groups', 0, '%s, %s' % (width, height))
return (width, height)
security.declarePublic('setPropertySheetAndDocument') security.declarePublic('setPropertySheetAndDocument')
def setPropertySheetAndDocument(self, def setPropertySheetAndDocument(self,
...@@ -844,10 +827,10 @@ class ManageCSS: ...@@ -844,10 +827,10 @@ class ManageCSS:
,properties_css_dict ,properties_css_dict
,page_iterator ,page_iterator
,page_id ,page_id
,page_height ,new_width
,page_width ,new_height
,width_groups ,old_width
,height_groups): ,old_height):
""" """
recover all CSS data relative to the current page and save these recover all CSS data relative to the current page and save these
information in the output dict information in the output dict
...@@ -875,13 +858,13 @@ class ManageCSS: ...@@ -875,13 +858,13 @@ class ManageCSS:
properties_css_page['margin-top'] = "%spx" % (40) properties_css_page['margin-top'] = "%spx" % (40)
# set width and height on page block # set width and height on page block
properties_css_page['width'] = str (page_width) + 'px' properties_css_page['width'] = str (new_width) + 'px'
properties_css_page['height'] = str (page_height) + 'px' properties_css_page['height'] = str (new_height) + 'px'
properties_page['actual_width'] = width_groups[page_iterator] properties_page['actual_width'] = old_width
properties_page['actual_height'] = height_groups[page_iterator] properties_page['actual_height'] = old_height
properties_css_background['height'] = str(page_height) + 'px' properties_css_background['height'] = str(new_height) + 'px'
properties_css_background['width'] = str (page_width) + 'px' properties_css_background['width'] = str (new_width) + 'px'
# adding properties dict to global dicts # adding properties dict to global dicts
properties_css_dict['head'][page_id] = properties_css_page properties_css_dict['head'][page_id] = properties_css_page
properties_css_dict['head'][background_id] = properties_css_background properties_css_dict['head'][background_id] = properties_css_background
...@@ -918,8 +901,8 @@ class ManageCSS: ...@@ -918,8 +901,8 @@ class ManageCSS:
# be found from the current's page top left corner. # be found from the current's page top left corner.
# that's why Y position must be updated # that's why Y position must be updated
scaling_factor1 = page_width/properties_page['actual_width'] scaling_factor1 = float(page_width)/properties_page['actual_width']
scaling_factor2 = page_height/properties_page['actual_height'] scaling_factor2 = float(page_height)/properties_page['actual_height']
properties_field['position_y'] = \ properties_field['position_y'] = \
str(float(properties_field['position_y']) - \ str(float(properties_field['position_y']) - \
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment