Created
January 2, 2011 13:08
-
-
Save luelista/762515 to your computer and use it in GitHub Desktop.
Gibt eine Umrechnungstabelle Fahrenheit->Celsius als Text auf der Standardausgabe oder als PDF-Dokument aus
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
# f2c-tab.pl | |
# Gibt eine Umrechnungstabelle Fahrenheit->Celsius als Text auf der Standard- | |
# ausgabe oder als PDF-Dokument aus | |
use strict; | |
use PDF::API2; | |
if (!(($#ARGV == 0 && $ARGV[0] == "--txt") || ($#ARGV == 1 && $ARGV[0] == "--pdf"))) { | |
print "perl f2c-tab.pl OPTIONS [FILENAME]\n"; | |
print " OPTIONS\n"; | |
print " --txt plain text to stdout\n"; | |
print " --pdf pdf document to given FILENAME\n"; | |
exit; | |
} | |
sub f2c { | |
return ($_[0] - 32) * 5 / 9; | |
} | |
my $out = ""; | |
for (my $i = 0; $i < 100; $i+=5) { | |
my $c = $i; $out.=sprintf " %3d F = %5.1f C ", $c, f2c($c); | |
my $c = $i + 100; $out.=sprintf " %3d F = %5.1f C ", $c, f2c($c); | |
my $c = $i + 200; $out.=sprintf " %3d F = %5.1f C ", $c, f2c($c); | |
my $c = $i + 300; $out.=sprintf " %3d F = %5.1f C ", $c, f2c($c); | |
$out.="\n"; | |
} | |
if ($#ARGV == 1 && $ARGV[0] == "--pdf") { | |
pdfOutput ($ARGV[1], "Umrechnungstabelle Fahrenheit --> Celsius", $out); | |
} else { | |
print $out; | |
} | |
sub pdfOutput { my ($filename, $heading, $text) = @_; | |
my $pdf = PDF::API2->new(-file => "$filename"); | |
$pdf->mediabox(595,842); | |
my $page = $pdf->page; | |
my $regular = $pdf->corefont('Courier',-encoding => 'latin1'); | |
my $bold = $pdf->corefont('Arial-Bold',-encoding => 'latin1'); | |
my $line = $page->gfx; | |
$line->strokecolor ("blue"); | |
$line->move (70,760); | |
$line->line (525,760); | |
$line->stroke; | |
my $txt = $page->text; | |
$txt->textstart; | |
$txt->font($bold, 20); | |
$txt->translate(70,770); | |
$txt->text($heading); | |
$txt->font($regular, 11); | |
my $startPos = 720; | |
my @lines = split "\n", $text; | |
while($_=shift @lines) { | |
$txt->translate(60,$startPos); $startPos-=10; | |
$txt->text($_); | |
} | |
$txt->textend; | |
$pdf->save; | |
$pdf->end( ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment