summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/PDF.pm
blob: cd244736e34df89a28a9f5c7805692981e537410 (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. } elsif ($type eq 'HASH') {
  40. for ( keys %{$rawvars} ) {
  41. $vars->{$_} = preprocess($rawvars->{$_});
  42. }
  43. } else {
  44. #XXX Fix escaping
  45. $rawvars =~ s/([&\$\\_<>~^#\%\{\}])/\\$1/g;
  46. $rawvars =~ s/"(.*)"/``$1''/gs;
  47. return $rawvars;
  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. }) || throw Error::Simple Template::Latex->error();
  62. if (not $template->process(
  63. get_template($parent->{template}),
  64. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  65. 'escape' => \&preprocess},
  66. "$parent->{outputfile}.pdf", binmode => 1)) {
  67. throw Error::Simple $template->error();
  68. }
  69. $parent->{mimetype} = 'application/pdf';
  70. }
  71. sub postprocess {
  72. my $parent = shift;
  73. $parent->{rendered} = "$parent->{outputfile}.pdf";
  74. return "$parent->{outputfile}.pdf";
  75. }
  76. 1;