summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/PDF.pm
blob: aca26f7104b2c6aefe90199cbb1035b90926108b (plain)
  1. =head1 NAME
  2. LedgerSMB::Template::PDF Template support module for LedgerSMB
  3. =head1 METHODS
  4. =over
  5. =item get_template ($name)
  6. Returns the appropriate template filename for this format.
  7. =item preprocess ($vars)
  8. Currently does nothing.
  9. =item process ($parent, $cleanvars)
  10. Processes the template for PDF.
  11. =item postprocess ($parent)
  12. Currently does nothing.
  13. =back
  14. =head1 Copyright (C) 2007, The LedgerSMB core team.
  15. This work contains copyrighted information from a number of sources all used
  16. with permission.
  17. It is released under the GNU General Public License Version 2 or, at your
  18. option, any later version. See COPYRIGHT file for details. For a full list
  19. including contact information of contributors, maintainers, and copyright
  20. holders, see the CONTRIBUTORS file.
  21. =cut
  22. package LedgerSMB::Template::PDF;
  23. use Error qw(:try);
  24. use Template::Latex;
  25. use LedgerSMB::Template::TTI18N;
  26. sub get_template {
  27. my $name = shift;
  28. return "${name}.tex";
  29. }
  30. sub preprocess {
  31. my $rawvars = shift;
  32. my $vars;
  33. my $type = ref $rawvars;
  34. return $rawvars if $type =~ /^LedgerSMB::Locale/;
  35. if ($type eq 'ARRAY') {
  36. for (@{$rawvars}) {
  37. push @{$vars}, preprocess($_);
  38. }
  39. } else (!$type) {
  40. #XXX Fix escaping
  41. $rawvars =~ s/([&\$\\_<>~^#\%\{\}])/\\$1/g;
  42. $rawvars =~ s/"(.*)"/``$1''/gs;
  43. return $rawvars;
  44. } else {
  45. for ( keys %{$rawvars} ) {
  46. $vars->{$_} = preprocess($rawvars->{$_});
  47. }
  48. }
  49. return $vars;
  50. }
  51. sub process {
  52. my $parent = shift;
  53. my $cleanvars = shift;
  54. my $template;
  55. $template = Template::Latex->new({
  56. LATEX_FORMAT => 'pdf',
  57. INCLUDE_PATH => $parent->{include_path},
  58. START_TAG => quotemeta('<?lsmb'),
  59. END_TAG => quotemeta('?>'),
  60. DELIMITER => ';'
  61. DEBUG => ($parent->{debug})? 'dirs': undef,
  62. DEBUG_FORMAT => '',
  63. }) || throw Error::Simple Template::Latex->error();
  64. if (not $template->process(
  65. get_template($parent->{template}),
  66. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  67. 'escape' => \&preprocess},
  68. "$parent->{outputfile}.pdf", binmode => 1)) {
  69. throw Error::Simple $template->error();
  70. }
  71. $parent->{mimetype} = 'application/pdf';
  72. }
  73. sub postprocess {
  74. my $parent = shift;
  75. $parent->{rendered} = "$parent->{outputfile}.pdf";
  76. return "$parent->{outputfile}.pdf";
  77. }
  78. 1;