Skip to content

Instantly share code, notes, and snippets.

@bokutin
Created April 24, 2012 18:24
Show Gist options
  • Save bokutin/2482337 to your computer and use it in GitHub Desktop.
Save bokutin/2482337 to your computer and use it in GitHub Desktop.
Strawerry Perlを使って帳票印刷をごまかす
use utf8;
use strict;
use feature ":5.10";
use Text::MicroTemplate;
use IO::All;
main: {
my $html_fn = 'chohyo.html';
my $pdf_fn = 'chohyo.pdf';
my $html = _html();
say "wrote: $html_fn";
io($html_fn)->utf8->print($html);
say "wrote: $pdf_fn";
_html_to_pdf($html_fn, $pdf_fn);
given ($ARGV[0]) {
when ("--preview") {
say "preview: $pdf_fn";
_preview($pdf_fn);
}
when ("--print") {
say "print: $pdf_fn";
_print($pdf_fn, $ARGV[1]);
}
}
}
sub _html {
my $html = Text::MicroTemplate->new( template => <<'HTML' )->build->();
<?
use List::Util qw(sum);
sub commify {
local $_ = shift;
1 while s/((?:\A|[^.0-9])[-+]?\d+)(\d{3})/$1,$2/s;
return $_;
}
my $date = "2012年4月25日(水)";
my @items = map {
{ name => "item$_", unit_price => 1000*$_, qty => $_%3==0 ? 2 : 1 };
} ( 1 .. 10 );
?>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="Content-Style-Type" content="text/css">
<style>
body {
/*
A4, http://detail.chiebukuro.yahoo.co.jp/qa/question_detail/q1412038274
*/
height: 1060px;
padding: 4% 8% 0% 8%;
}
#container-table {
width: 100%;
}
#title {
font-size: xx-large;
}
#item-table {
width: 100%;
border-collapse: collapse;
}
#item-table th, #item-table td {
border: 1px solid black;
}
#item-table th.first, #item-table td.first {
border-left: none;
}
#item-table th.last, #item-table td.last {
border-right: none;
}
#item-table tr.top th {
border-top: none;
}
#item-table tr.bottom th {
border-bottom: none;
}
tr.bgcolor {
background-color: #447BA3;
color: white;
font-weight: bold;
line-height: 150%;
}
th.number, td.number {
text-align: right;
}
td.subtotal {
text-align: right;
padding-right: 1em;
font-size: x-small;
}
</style>
</head>
<body>
<table id="container-table">
<tr>
<td style="width: 30%; vertical-align: bottom;">
<span id="title">見積書</span>
</td>
<td style="width: 70%; vertical-align: bottom;">
<span id="corp">○○会社 ○○商事 御中</span>
</td>
</tr>
<tr>
<td>
</td>
<td style="padding: 10px;">
<span id="date">日付 <?= $date ?></h1>
</td>
</tr>
<tr>
<td style="vertical-align: top;">
<div style="margin-top: 10%;">
○○会社 ○○建設<br />
郵便番号 xxx-xxxx<br />
○○県 ○○市 xxx<br />
TEL 0120-xxx-xxxx<br />
</div>
</td>
<td>
<table id="item-table">
<tr class="bgcolor top">
<th class="first">項目</th>
<th>数</th>
<th>単価</th>
<th class="last">金額</th>
</tr>
? for my $item (@items) {
<tr>
<td class="first"><?= $item->{name} ?></td>
<td><?= $item->{qty} ?></td>
<td class="number"><?= commify $item->{unit_price} ?></td>
<td class="number last"><?= commify $item->{unit_price}*$item->{qty} ?></td>
</tr>
? }
<tr>
<td colspan="3" class="subtotal first">
小計
</td>
<td class="number last">
? my $sum = sum( map { $_->{unit_price}*$_->{qty} } @items );
<?= commify $sum ?>
</td>
</tr>
<tr>
? my $tax = int( $sum * 0.05 );
<td colspan="3" class="subtotal first">
税額 5.00%
</td>
<td class="number last">
<?= commify $tax ?>
</td>
</tr>
<tr class="bgcolor bottom">
<th colspan="3" class="first">
合計
</th>
<th class="number last">
<?= commify $sum + $tax ?>
</th>
</tr>
</table>
</td>
</tr>
<tr>
<td>
</td>
<td style="padding: 10px;">
<p>
備考
</p>
<p>
1行目<br />
2行目<br />
3行目<br />
</p>
</td>
</tr>
</table>
</body>
</html>
HTML
}
sub _html_to_pdf {
my ($html_fn, $pdf_fn) = @_;
my $exe = File::Spec->catfile('contrib\wkhtmltopdf\wkhtmltopdf.exe');
my @cmd = ($exe, '--quiet', '--page-size', 'A4', $html_fn, $pdf_fn);
system(@cmd);
}
sub _preview {
my ($fn) = @_;
my $exe = File::Spec->catfile('contrib\SumatraPDF\SumatraPDF.exe');
my @cmd = ($exe, $fn);
system(@cmd);
}
sub _print {
my ($fn, $printer_name) = @_;
my $exe = File::Spec->catfile('contrib\SumatraPDF\SumatraPDF.exe');
my @cmd = ($exe, '-exit-on-print');
if ($printer_name) {
push @cmd, '-print-to', $printer_name, $fn;
}
else {
push @cmd, '-print-to-default', $fn;
}
system(@cmd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment