<s> """<STR_LIT>""" <EOL> from future import standard_library <EOL> standard_library . install_aliases ( ) <EOL> import logging <EOL> import os <EOL> import io <EOL> from django . conf import settings <EOL> from ho import pisa <EOL> from lino . core . web import extend_context <EOL> from lino . utils . appy_pod import PrintTableAction <EOL> from lino . core . tables import AbstractTable <EOL> from lino . utils . media import TmpMediaFile <EOL> from lino . utils . xmlgen import html as xghtml <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> class PrintTableActionPisa ( PrintTableAction ) : <EOL> template_name = "<STR_LIT>" <EOL> def run_from_ui ( self , ar , ** kw ) : <EOL> mf = TmpMediaFile ( ar , self . target_file_format ) <EOL> settings . SITE . makedirs_if_missing ( os . path . dirname ( mf . name ) ) <EOL> self . render ( ar , mf . name ) <EOL> ar . response . update ( success = True ) <EOL> ar . response . update ( open_url = mf . url ) <EOL> def as_html_table ( self , ar ) : <EOL> t = xghtml . Table ( ) <EOL> ar . dump2html ( t , ar . data_iterator ) <EOL> return xghtml . E . tostring ( t . as_element ( ) ) <EOL> def render ( self , ar , output_file ) : <EOL> context = { <EOL> '<STR_LIT:action>' : self , <EOL> '<STR_LIT>' : ar , <EOL> '<STR_LIT>' : self . as_html_table , <EOL> } <EOL> extend_context ( context ) <EOL> template = settings . SITE . plugins . jinja . renderer . jinja_env . get_template ( <EOL> self . template_name ) <EOL> html = template . render ( ** context ) . encode ( '<STR_LIT:utf-8>' ) <EOL> with open ( output_file + '<STR_LIT>' , "<STR_LIT:w>" ) as file : <EOL> file . write ( html ) <EOL> result = io . StringIO ( ) <EOL> h = logging . FileHandler ( output_file + '<STR_LIT>' , '<STR_LIT:w>' ) <EOL> pisa . log . addHandler ( h ) <EOL> pdf = pisa . pisaDocument ( <EOL> io . StringIO ( html ) , result , encoding = '<STR_LIT:utf-8>' ) <EOL> pisa . log . removeHandler ( h ) <EOL> h . close ( ) <EOL> with open ( output_file , '<STR_LIT:wb>' ) as file : <EOL> file . write ( result . getvalue ( ) ) <EOL> file . close ( ) <EOL> if pdf . err : <EOL> raise Exception ( "<STR_LIT>" % pdf . err ) <EOL> class PortraitPrintTableActionPisa ( PrintTableActionPisa ) : <EOL> label = _ ( "<STR_LIT>" ) <EOL> sort_index = - <NUM_LIT:9> <EOL> template_name = "<STR_LIT>" <EOL> AbstractTable . as_pdf = PrintTableActionPisa ( ) <EOL> AbstractTable . as_pdf_p = PortraitPrintTableActionPisa ( ) </s>
