X-Git-Url: https://git.mdrn.pl/librarian.git/blobdiff_plain/716a9ab552bffbb7df2cb31ae41ee196902c7653..67a177f2ec1fa2eac56e7fb07ccaf32bcd33d8ce:/src/librarian/pdf.py diff --git a/src/librarian/pdf.py b/src/librarian/pdf.py index a51dbb5..a025b9b 100644 --- a/src/librarian/pdf.py +++ b/src/librarian/pdf.py @@ -20,6 +20,7 @@ from copy import deepcopy from subprocess import call, PIPE from itertools import chain +from PIL import Image from Texml.processor import process from lxml import etree from lxml.etree import XMLSyntaxError, XSLTApplyError @@ -242,7 +243,7 @@ def package_available(package, args='', verbose=False): def transform(wldoc, verbose=False, save_tex=None, morefloats=None, - cover=None, flags=None, customizations=None, ilustr_path='', + cover=None, flags=None, customizations=None, base_url='file://./', latex_dir=False): """ produces a PDF file with XeLaTeX @@ -314,8 +315,30 @@ def transform(wldoc, verbose=False, save_tex=None, morefloats=None, # TeXML -> LaTeX temp = mkdtemp('-wl2pdf') - for ilustr in document.edoc.findall("//ilustr"): - shutil.copy(os.path.join(ilustr_path, ilustr.get("src")), temp) + for i, ilustr in enumerate(document.edoc.findall('//ilustr')): + url = six.moves.urllib.parse.urljoin( + base_url, + ilustr.get('src') + ) + imgfile = six.moves.urllib.request.urlopen(url) + img = Image.open(imgfile) + + th_format, ext, media_type = { + 'GIF': ('GIF', 'gif', 'image/gif'), + 'PNG': ('PNG', 'png', 'image/png'), + }.get(img.format, ('JPEG', 'jpg', 'image/jpeg')) + + width = 2400 + if img.size[0] < width: + th = img + else: + th = img.resize((width, round(width * img.size[1] / img.size[0]))) + + file_name = 'image%d.%s' % (i, ext) + th.save(os.path.join(temp, file_name)) + ilustr.set('src', file_name) + + imgfile.close() for sponsor in book_info.sponsors: ins = etree.Element("data-sponsor", name=sponsor)