Projekt

Allgemein

Profil

« Zurück | Weiter » 

Revision 1436ca8d

Von Moritz Bunkus vor mehr als 11 Jahren hinzugefügt

  • ID 1436ca8d2ccc46bec6729cbcbf3b0ee284e57161
  • Vorgänger 5e9aaf1c
  • Nachfolger b3219da5

SL::Template::LaTeX: Funktion zum Parsen & PDF erzeugen aus einem .tex mit nur einem Funktionsaufruf

Unterschiede anzeigen:

SL/Form.pm
998 998
    $ext_for_format = $self->{"format"} =~ m/pdf/ ? 'pdf' : 'odt';
999 999

  
1000 1000
  } elsif ($self->{"format"} =~ /(postscript|pdf)/i) {
1001
    $ENV{"TEXINPUTS"} = ".:" . getcwd() . "/" . $myconfig->{"templates"} . ":" . $ENV{"TEXINPUTS"};
1002 1001
    $template_type    = 'LaTeX';
1003 1002
    $ext_for_format   = 'pdf';
1004 1003

  
SL/Template/LaTeX.pm
4 4

  
5 5
use strict;
6 6

  
7
use Carp;
7 8
use Cwd;
9
use English qw(-no_match_vars);
10
use File::Basename;
11
use File::Temp;
12
use List::MoreUtils qw(any);
8 13
use Unicode::Normalize qw();
9 14

  
10 15
sub new {
......
304 309
      $used_packages{$1} = 1;
305 310
      $last_usepackage_line = $i;
306 311

  
307
    } elsif ($lines->[$i] =~ m/\\begin{document}/) {
312
    } elsif ($lines->[$i] =~ m/\\begin\{document\}/) {
308 313
      $document_start_line = $i;
309 314
      last;
310 315

  
......
328 333
  my $form = $self->{"form"};
329 334

  
330 335
  if (!open(IN, "$form->{templates}/$form->{IN}")) {
331
    $self->{"error"} = "$!";
336
    $self->{"error"} = "$form->{templates}/$form->{IN}: $!";
332 337
    return 0;
333 338
  }
334 339
  binmode IN, ":utf8" if $::locale->is_utf8;
......
354 359

  
355 360
  my $new_contents;
356 361
  if ($self->{use_template_toolkit}) {
357
    my $additional_params = $::form;
358

  
359 362
    if ($self->{custom_tag_style}) {
360 363
      $contents = "[% TAGS $self->{tag_start} $self->{tag_end} %]\n" . $contents;
361 364
    }
362 365

  
363
    $::form->init_template->process(\$contents, $additional_params, \$new_contents) || die $::form->template->error;
366
    $::form->init_template->process(\$contents, $form, \$new_contents) || die $::form->template->error;
364 367
  } else {
365 368
    $new_contents = $self->parse_block($contents);
366 369
  }
......
391 394
  my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
392 395

  
393 396
  # Convert the tex file to postscript
397
  local $ENV{TEXINPUTS} = ".:" . $form->{cwd} . "/" . $form->{templates} . ":" . $ENV{TEXINPUTS};
394 398

  
395 399
  if (!chdir("$userspath")) {
396 400
    $self->{"error"} = "chdir : $!";
......
440 444
  my ($form, $userspath) = ($self->{"form"}, $self->{"userspath"});
441 445

  
442 446
  # Convert the tex file to PDF
447
  local $ENV{TEXINPUTS} = ".:" . $form->{cwd} . "/" . $form->{templates} . ":" . $ENV{TEXINPUTS};
443 448

  
444 449
  if (!chdir("$userspath")) {
445 450
    $self->{"error"} = "chdir : $!";
......
471 476
  $form->{tmpfile} =~ s/tex$/pdf/;
472 477

  
473 478
  $self->cleanup();
479

  
480
  return 1;
474 481
}
475 482

  
476 483
sub _get_latex_path {
......
491 498
  return 1;
492 499
}
493 500

  
501
sub parse_and_create_pdf {
502
  my ($class, $template_file_name, %params) = @_;
503

  
504
  my $keep_temp                = $::lx_office_conf{debug} && $::lx_office_conf{debug}->{keep_temp_files};
505
  my ($tex_fh, $tex_file_name) = File::Temp::tempfile(
506
    'kivitendo-printXXXXXX',
507
    SUFFIX => '.tex',
508
    DIR    => $::lx_office_conf{paths}->{userspath},
509
    UNLINK => $keep_temp ? 0 : 1,,
510
  );
511

  
512
  my $old_wd               = getcwd();
513

  
514
  my $local_form           = Form->new('');
515
  $local_form->{cwd}       = $old_wd;
516
  $local_form->{IN}        = $template_file_name;
517
  $local_form->{tmpdir}    = $::lx_office_conf{paths}->{userspath};
518
  $local_form->{tmpfile}   = $tex_file_name;
519
  $local_form->{templates} = $::myconfig{templates};
520

  
521
  foreach (keys %params) {
522
    croak "The parameter '$_' must not be used." if exists $local_form->{$_};
523
    $local_form->{$_} = $params{$_};
524
  }
525

  
526
  my $error;
527
  eval {
528
    my $template = SL::Template::LaTeX->new($template_file_name, $local_form, \%::myconfig, $::lx_office_conf{paths}->{userspath});
529
    my $result   = $template->parse($tex_fh) && $template->convert_to_pdf;
530

  
531
    die $template->{error} unless $result;
532

  
533
    1;
534
  } or do { $error = $EVAL_ERROR; };
535

  
536
  chdir $old_wd;
537
  close $tex_fh;
538

  
539
  if ($keep_temp) {
540
    chmod(((stat $tex_file_name)[2] & 07777) | 0660, $tex_file_name);
541
  } else {
542
    my $tmpfile =  $tex_file_name;
543
    $tmpfile    =~ s/\.\w+$//;
544
    unlink(grep { !m/\.pdf$/ } <$tmpfile.*>);
545
  }
546

  
547
  return (error     => $error) if $error;
548
  return (file_name => do { $tex_file_name =~ s/tex$/pdf/; $tex_file_name });
549
}
550

  
494 551
1;
SL/Template/Plugin/LxLatex.pm
1 1
package SL::Template::Plugin::LxLatex;
2 2

  
3 3
use strict;
4
use parent qw( Template::Plugin );
4
use parent qw( Template::Plugin::Filter );
5 5

  
6 6
my $cached_instance;
7 7

  
......
14 14
sub init {
15 15
  my $self = shift;
16 16

  
17
  $self->install_filter($self->{ _ARGS }->[0] || 'T8');
17
  $self->install_filter($self->{ _ARGS }->[0] || 'LxLatex');
18 18

  
19 19
  return $self;
20 20
}

Auch abrufbar als: Unified diff