Commit 9b162d39 authored by Fabien Morin's avatar Fabien Morin

rewriting of the setBackgroundPictures method :

- add a file format attribute that permit to chose the format ImageMagick will
  convert the background into (now it's easy to change in png or jpg, or 
  whatever convert could support)
- remove a lot of unused code
- remove dangerous code !
- enhance security on file deletion by removing only the files used during this
  method execution (and not using rm -f /tmp/tmp* like it was)
- use only one convert command (from ImageMagick) so the pdftoppm command 
  (from Xpdf) it's not yet required in this method (but there is others 
  points to enhance in the file) so requirements for this module will be less
  importants.
- add some logs if the command status output is not 0
- with this enhancements, it's not yet necesary to set "Desired Width" AND
  "Desired Height" in the module creation user interface. Just one is required 
  (but it's always possible to set the two's), convert will determine the 
  other dimention using proportion if there is only one.


git-svn-id: https://svn.erp5.org/repos/public/erp5/trunk@18932 20353a03-c40f-0410-a6d1-a30d3c3de9de
parent d82dc11a
...@@ -35,7 +35,7 @@ from AccessControl import ClassSecurityInfo ...@@ -35,7 +35,7 @@ from AccessControl import ClassSecurityInfo
from Globals import InitializeClass, get_request from Globals import InitializeClass, get_request
from zipfile import ZipFile, ZIP_DEFLATED from zipfile import ZipFile, ZIP_DEFLATED
from StringIO import StringIO from StringIO import StringIO
from zLOG import LOG from zLOG import LOG, TRACE, WARNING, ERROR, INFO
import imghdr import imghdr
import random import random
import getopt, sys, os import getopt, sys, os
...@@ -556,91 +556,92 @@ class ManageFiles: ...@@ -556,91 +556,92 @@ class ManageFiles:
skin_folder, skin_folder,
desired_height, desired_height,
desired_width, desired_width,
resolution resolution,
background_format
): ):
""" """
extract background pictures from pdf file and convert them extract background pictures from pdf file and convert them
in the right format (JPEG) and save them in the corresponding in the right format (JPEG) and save them in the corresponding
folder (skin_folder). folder (skin_folder).
to work, this procedure needs to have pdftoppm (from Xpdf) to work, this procedure needs convert (from ImageMagick)
and convert (from ImageMagick) installed on the server installed on the server otherwise nothing is created.
otherwise nothing is created. Temp files wich are created are deleted once the job is done.
Temp files are created in the '/tmp/' folder, and are deleted At the end, return the properties (size_x, size_y) the background
once the job is done. images have.
At the end, get the properties (size_x, size_y) of the first
image (i.e page_0) and returns them.
""" """
import commands import commands
import tempfile
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
#temp_test= NamedTemporaryFile(mode= "w+b") temp_pdf = NamedTemporaryFile()
#tempFile= NamedTemporaryFile().name
ScribusUtilsTempPDF= NamedTemporaryFile(mode= "w+b")
ScribusUtilstempsPDFName= NamedTemporaryFile().name
# going to the begining of the input file
# XXX - this is really bad because the appropriate # opening new file on HDD to save created background image content
# way to run zope is to create a local instance temp_image = NamedTemporaryFile()
# it should be removed XXX - some people # this is made to have a name not yet used by the system
# do this just to make sure "it works" temp_image_name = temp_image.name + background_format
# but it is not even multiplatform
os.putenv('TMPDIR', '/tmp')
# saving content
temp_pdf = open(ScribusUtilstempsPDFName,'w')
# going to the begining of the input file # going to the begining of the input file
pdf_file.seek(0) pdf_file.seek(0)
# saving content # saving content
# saving content
temp_pdf.write(pdf_file.read()) temp_pdf.write(pdf_file.read())
temp_pdf.seek(0)
result = commands.getstatusoutput('convert -density %s -resize %sx%s '\
'%s %s' % (resolution, desired_width, desired_height, temp_pdf.name,
background_format + ':' + temp_image_name))
# check that the command has been done succeful
if result[0] != 0:
LOG('ScribusUtils.setBackgroundPictures :', ERROR, 'convert command'\
'failed with the following error message : \n%' % result[1])
temp_pdf.close() temp_pdf.close()
# launching first soft to convert from PDF to PPM background_image_list = []
ScribusUtilstempsPPM = NamedTemporaryFile(mode="w+b") # convert add a '-N' string a the end of the file name if there is more
ScribusUtilstempsPPMName = NamedTemporaryFile().name # than one page in the pdf file (where N is the number of the page,
result = commands.getstatusoutput('pdftoppm -r %s %s %s' % \ # begining at 0)
(resolution, ScribusUtilstempsPDFName, ScribusUtilstempsPPMName)) if os.path.exists(temp_image_name):
# launching second soft to convert from PPM to JPEG # thats mean there's only one page in the pdf file
ScribusUtilstempsJPG = NamedTemporaryFile(mode="w+b") background_image_list.append(temp_image_name)
ScribusUtilstempsJPGName = NamedTemporaryFile().name else:
# in the case of multi-pages pdf file, we must find all files
image_number = 0
while os.path.exists(temp_image_name + '-%s' % image_number):
background_image_list.append(temp_image_name + '-%s' % image_number)
image_number += 1
original_result= commands.getstatusoutput('identify %s' % \ if not len(background_image_list):
(ScribusUtilstempsPDFName)) LOG('ScribusUtils.setBackgroundPictures :', ERROR, 'no background '\
result = commands.getstatusoutput('convert -density %s -resize %sx%s '\ 'image found')
'%s %s' % (resolution,desired_width,desired_height,
ScribusUtilstempsPPMName + '*', 'jpg:' + ScribusUtilstempsJPGName)) # get the real size of the first image
# this could be usefull if the user only defined one dimention
number = ScribusUtilstempsJPGName.find('tmp') # and convert has calculate the other using proportionnality
directory_tmp= ScribusUtilstempsJPGName[:(number+4)] file_result= commands.getstatusoutput('identify %s' %\
background_image_list[0])
# getting list of JPG output files
result = commands.getstatusoutput('ls %s | grep %s' % (directory_tmp, real_size_x = file_result[1].split(' ')[2].split('x')[1]
ScribusUtilstempsJPGName.split('/')[-1])) real_size_y = file_result[1].split(' ')[2].split('x')[0]
# deleting all temporary files
# getting the original size of the file # add images in erp5 :
real_size_x= 0
real_size_y= 0
image_number = 0 image_number = 0
if result[1] != '': addImageMethod = skin_folder.manage_addProduct['OFSP'].manage_addImage
# result[1] contains the output string from the command, for background_image in background_image_list:
# in our case the result of the ls. temp_background_file = open(background_image, 'r')
# splitting this string to get the list of objects
for image in result[1].split('\n'):
temp_jpg = open('%s%s' % (directory_tmp, image), 'r')
form_page_id = object_names['page'] + str(image_number) form_page_id = object_names['page'] + str(image_number)
addImage = skin_folder.manage_addProduct['OFSP'].manage_addImage addImageMethod(form_page_id, temp_background_file, "background image")
addImage(form_page_id, temp_jpg, "background image")
image_number += 1 image_number += 1
# deleting all temporary files
result = commands.getstatusoutput('rm -f /tmp/tmp*') # JPS-XXX Extremely
# dangerous
# open page_0's final background picture to recover size_x and size_y
final_image = getattr(skin_folder, object_names['page'] + '0')
size_x = desired_height
size_y = desired_width
return (size_x, size_y, real_size_x, real_size_y) # remove the file from the system
result = commands.getstatusoutput('rm -f %s' % background_image)
size_x = int(real_size_x)
size_y = int(real_size_y)
LOG('ScribusUtils.setBackgroundPictures :', INFO,
'return size : x=%s, y=%s' % size_x, size_y)
return (size_x, size_y)
security.declarePublic('getPageattributes') security.declarePublic('getPageattributes')
def getPageattributes (self, def getPageattributes (self,
...@@ -783,8 +784,6 @@ class ManageCSS: ...@@ -783,8 +784,6 @@ class ManageCSS:
,page_id ,page_id
,page_height ,page_height
,page_width ,page_width
,original_page_width
,original_page_height
,width_groups,height_groups): ,width_groups,height_groups):
""" """
recover all CSS data relative to the current page and save these recover all CSS data relative to the current page and save these
...@@ -847,9 +846,9 @@ class ManageCSS: ...@@ -847,9 +846,9 @@ class ManageCSS:
, page_iterator , page_iterator
, page_gap , page_gap
, keep_page , keep_page
, original_page_width , properties_page
, original_page_height , actual_width
, properties_page,actual_width,actual_height): ,actual_height):
""" """
recover all CSS data relative to the current page_object (field) recover all CSS data relative to the current page_object (field)
and save these informations in the output dict and save these informations in the output dict
......
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