summaryrefslogtreecommitdiff
path: root/LedgerSMB/Template/PDF.pm
blob: f201b11dd266b2a4fe6be4fca89686a267991921 (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. sub get_template {
  26. my $name = shift;
  27. return "${name}.tex";
  28. }
  29. sub preprocess {
  30. my $rawvars = shift;
  31. my $vars;
  32. my $type = ref $rawvars;
  33. if ($type eq 'ARRAY') {
  34. for (@{$rawvars}) {
  35. push @{$vars}, preprocess($_);
  36. }
  37. } elsif ($type eq 'HASH') {
  38. for ( keys %{$rawvars} ) {
  39. $vars->{$_} = preprocess($rawvars->{$_});
  40. }
  41. } else {
  42. #XXX Fix escaping
  43. $rawvars =~ s/([&\$\\_<>~^#\%\{\}])/\\$1/g;
  44. $rawvars =~ s/"(.*)"/``$1''/gs;
  45. return $rawvars;
  46. }
  47. return $vars;
  48. }
  49. sub process {
  50. my $parent = shift;
  51. my $cleanvars = shift;
  52. my $template;
  53. $template = Template::Latex->new({
  54. LATEX_FORMAT => 'pdf',
  55. INCLUDE_PATH => $parent->{include_path},
  56. START_TAG => quotemeta('<?lsmb'),
  57. END_TAG => quotemeta('?>'),
  58. DELIMITER => ';'
  59. }) || throw Error::Simple Template::Latex->error();
  60. if (not $template->process(
  61. get_template($parent->{template}),
  62. $cleanvars, "$parent->{outputfile}.pdf", binmode => 1)) {
  63. throw Error::Simple $template->error();
  64. }
  65. $parent->{mimetype} = 'application/pdf';
  66. }
  67. sub postprocess {
  68. my $parent = shift;
  69. $parent->{rendered} = "$parent->{outputfile}.pdf";
  70. return "$parent->{outputfile}.pdf";
  71. }
  72. 1;