Created
February 16, 2018 15:54
-
-
Save jef-sure/4a4d3487b2b525345b2f0e492c1924c7 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| use PDF::Haru; | |
| use strict; | |
| use warnings; | |
| # create new document | |
| my $pdf = PDF::Haru::New(); | |
| #$pdf->LoadTTFontFromFile("/usr/share/fonts/truetype/msttcorefonts/arial.ttf", HPDF_TRUE); | |
| # add page | |
| my $page = $pdf->AddPage(); | |
| # set page size and orientation | |
| $page->SetSize(HPDF_PAGE_SIZE_A4, HPDF_PAGE_PORTRAIT); | |
| #my $font = $pdf->GetFont("ArialMT", "StandardEncoding"); | |
| my $font = $pdf->GetFont("Helvetica", "StandardEncoding"); | |
| $page->SetFontAndSize($font, 22); | |
| my $page_width = $page->GetWidth(); | |
| my $page_height = $page->GetHeight(); | |
| my $pixes_in_mm = $page->GetWidth() / 210; | |
| my $zero_width = $page->TextWidth("0"); | |
| my $template_width = $page->TextWidth("0" x 6); | |
| sub mm_to_pix { | |
| return $_[0] * $pixes_in_mm; | |
| } | |
| my %eqset; | |
| my $max = 50; | |
| sub make_equation { | |
| for (1 .. 5) { | |
| my $sign = ("-", "+")[rand 2]; | |
| my ($a, $b); | |
| if ($sign eq "-") { | |
| $a = 1 + int rand $max; | |
| $b = 1 + int rand $a - 1; | |
| } else { | |
| $a = 1 + int rand $max - 1; | |
| $b = 1 + int rand $max - 2 - $a; | |
| } | |
| my $eq = "$a $sign $b"; | |
| if (!exists $eqset{$eq}) { | |
| $eqset{$eq} = undef; | |
| return $eq; | |
| } | |
| } | |
| } | |
| sub draw_equation { | |
| my ($x, $y) = @_; | |
| my $text = make_equation(); | |
| my $tw = $page->TextWidth($text); | |
| my $xo = ($template_width - $tw) / 2; | |
| $page->BeginText(); | |
| $page->TextOut($x + $xo, $y, $text); | |
| $page->TextOut($x + $template_width, $y, "="); | |
| $page->EndText(); | |
| $page->MoveTo($x + $template_width + $zero_width + mm_to_pix(2), $y + mm_to_pix(-1)); | |
| $page->LineTo($x + $template_width + $zero_width * 4, $y + mm_to_pix(-1)); | |
| $page->Stroke(); | |
| } | |
| for (my $x = mm_to_pix(20); $x < $page_width - int mm_to_pix 70; $x += int mm_to_pix 57) { | |
| for (my $y = mm_to_pix(13); $y < $page_height - mm_to_pix(30); $y += int mm_to_pix 15) { | |
| draw_equation($x, $y); | |
| } | |
| } | |
| my $bottom = 10; | |
| my $top = 297 - 20; | |
| my $left = 15; | |
| my $right = 210 - 15; | |
| my $radius = 3; | |
| $page->Arc(mm_to_pix($left + $radius), mm_to_pix($bottom + $radius), mm_to_pix($radius), 180, 270); | |
| $page->Stroke(); | |
| $page->Arc(mm_to_pix($right - $radius), mm_to_pix($bottom + $radius), mm_to_pix($radius), 90, 180); | |
| $page->Stroke(); | |
| $page->Arc(mm_to_pix($left + $radius), mm_to_pix($top - $radius), mm_to_pix($radius), 270, 360); | |
| $page->Stroke(); | |
| $page->Arc(mm_to_pix($right - $radius), mm_to_pix($top - $radius), mm_to_pix($radius), 0, 90); | |
| $page->Stroke(); | |
| $page->MoveTo(mm_to_pix($right - $radius), mm_to_pix($bottom)); | |
| $page->LineTo(mm_to_pix($left + $radius), mm_to_pix($bottom)); | |
| $page->MoveTo(mm_to_pix($right - $radius), mm_to_pix($top)); | |
| $page->LineTo(mm_to_pix($left + $radius), mm_to_pix($top)); | |
| $page->MoveTo(mm_to_pix($left), mm_to_pix($top - $radius)); | |
| $page->LineTo(mm_to_pix($left), mm_to_pix($bottom + $radius)); | |
| $page->MoveTo(mm_to_pix($right), mm_to_pix($top - $radius)); | |
| $page->LineTo(mm_to_pix($right), mm_to_pix($bottom + $radius)); | |
| $page->Stroke(); | |
| # save the document to a file | |
| $pdf->SaveToFile("filename.pdf"); | |
| # cleanup | |
| $pdf->Free(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment