<s> import re <EOL> import os <EOL> import cPickle <EOL> import gluon . serializers <EOL> from gluon import current , HTTP <EOL> from gluon . html import markmin_serializer , TAG , HTML , BODY , UL , XML , H1 <EOL> from gluon . contenttype import contenttype <EOL> from gluon . contrib . fpdf import FPDF , HTMLMixin <EOL> from gluon . sanitizer import sanitize <EOL> from gluon . contrib . markmin . markmin2latex import markmin2latex <EOL> from gluon . contrib . markmin . markmin2pdf import markmin2pdf <EOL> def wrapper ( f ) : <EOL> def g ( data ) : <EOL> try : <EOL> output = f ( data ) <EOL> return XML ( ouput ) <EOL> except ( TypeError , ValueError ) , e : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' % e ) <EOL> except ImportError , e : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' % e ) <EOL> except Exception , e : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' % e ) <EOL> return g <EOL> def latex_from_html ( html ) : <EOL> markmin = TAG ( html ) . element ( '<STR_LIT:body>' ) . flatten ( markmin_serializer ) <EOL> return XML ( markmin2latex ( markmin ) ) <EOL> def pdflatex_from_html ( html ) : <EOL> if os . system ( '<STR_LIT>' ) == <NUM_LIT:0> : <EOL> markmin = TAG ( html ) . element ( '<STR_LIT:body>' ) . flatten ( markmin_serializer ) <EOL> out , warnings , errors = markmin2pdf ( markmin ) <EOL> if errors : <EOL> current . response . headers [ '<STR_LIT:Content-Type>' ] = '<STR_LIT>' <EOL> raise HTTP ( <NUM_LIT> , HTML ( BODY ( H1 ( '<STR_LIT>' ) , <EOL> UL ( * errors ) , <EOL> H1 ( '<STR_LIT>' ) , <EOL> UL ( * warnings ) ) ) . xml ( ) ) <EOL> else : <EOL> return XML ( out ) <EOL> def pyfpdf_from_html ( html ) : <EOL> request = current . request <EOL> def image_map ( path ) : <EOL> if path . startswith ( '<STR_LIT>' % request . application ) : <EOL> return os . path . join ( request . folder , path . split ( '<STR_LIT:/>' , <NUM_LIT:2> ) [ <NUM_LIT:2> ] ) <EOL> return '<STR_LIT>' % ( request . is_https and '<STR_LIT:s>' or '<STR_LIT>' , request . env . http_host , path ) <EOL> class MyFPDF ( FPDF , HTMLMixin ) : <EOL> pass <EOL> pdf = MyFPDF ( ) <EOL> pdf . add_page ( ) <EOL> html = sanitize ( <EOL> html , escape = False ) <EOL> pdf . write_html ( html , image_map = image_map ) <EOL> return XML ( pdf . output ( dest = '<STR_LIT:S>' ) ) <EOL> def pdf_from_html ( html ) : <EOL> if os . system ( '<STR_LIT>' ) == <NUM_LIT:0> : <EOL> return pdflatex_from_html ( html ) <EOL> else : <EOL> return pyfpdf_from_html ( html ) </s>
