summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/PDF.pm
blob: ef7ca519479fdd6b37a36b830e054e086811e083 (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) {
  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. my $source;
  56. $parent->{outputfile} ||=
  57. "${LedgerSMB::Sysconfig::tempdir}/$parent->{template}-output-$$";
  58. if (ref $parent->{template} eq 'SCALAR') {
  59. $source = $parent->{template};
  60. } elsif (ref $parent->{template} eq 'ARRAY') {
  61. $source = join "\n", @{$parent->{template}};
  62. } else {
  63. $source = get_template($parent->{template});
  64. }
  65. $template = Template::Latex->new({
  66. LATEX_FORMAT => 'pdf',
  67. INCLUDE_PATH => $parent->{include_path},
  68. START_TAG => quotemeta('<?lsmb'),
  69. END_TAG => quotemeta('?>'),
  70. DELIMITER => ';',
  71. DEBUG => ($parent->{debug})? 'dirs': undef,
  72. DEBUG_FORMAT => '',
  73. }) || throw Error::Simple Template::Latex->error();
  74. if (not $template->process(
  75. $source,
  76. {%$cleanvars, %$LedgerSMB::Template::TTI18N::ttfuncs,
  77. 'escape' => \&preprocess},
  78. "$parent->{outputfile}.pdf", binmode => 1)) {
  79. throw Error::Simple $template->error();
  80. }
  81. $parent->{mimetype} = 'application/pdf';
  82. }
  83. sub postprocess {
  84. my $parent = shift;
  85. $parent->{rendered} = "$parent->{outputfile}.pdf";
  86. return "$parent->{outputfile}.pdf";
  87. }
  88. 1;