Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions phpdotnet/phd/Format/Abstract/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,10 @@ class PdfWriter {

function __construct($pageWidth = 210, $pageHeight = 297) {
// Initialization of properties
$this->haruDoc = new \HaruDoc;
$this->haruDoc->addPageLabel(1, \HaruPage::NUM_STYLE_DECIMAL, 1, "Page ");
$this->haruDoc = new Haru_HaruDoc;
$this->haruDoc->addPageLabel(1, Haru_HaruPage::NUM_STYLE_DECIMAL, 1, "Page ");

$this->haruDoc->setPageMode(\HaruDoc::PAGE_MODE_USE_OUTLINE);
$this->haruDoc->setPageMode(Haru_HaruDoc::PAGE_MODE_USE_OUTLINE);
$this->haruDoc->setPagesConfiguration(2);

// Page format
Expand All @@ -157,7 +157,7 @@ function __construct($pageWidth = 210, $pageHeight = 297) {
$this->currentFontSize = 12;
$this->currentFontColor = array(0, 0, 0); // Black
$this->nextPage();
$this->haruDoc->addPageLabel(1, \HaruPage::NUM_STYLE_DECIMAL, 1, "Page ");
$this->haruDoc->addPageLabel(1, Haru_HaruPage::NUM_STYLE_DECIMAL, 1, "Page ");
}

public function getCurrentPage() {
Expand Down Expand Up @@ -241,7 +241,7 @@ public function appendText($text) {
$this->PAGE_HEIGHT - (self::VMARGIN + $this->vOffset), $textToAppend);
}
if ($textToAppend)
$this->current["char"] = $textToAppend{strlen($textToAppend)-1};
$this->current["char"] = $textToAppend[strlen($textToAppend)-1];

// Offsets for next line
if (!$isLastLine) {
Expand Down Expand Up @@ -302,7 +302,7 @@ public function appendOneLine($text) {
$this->currentPage->textOut(self::HMARGIN + $this->hOffset + $this->permanentLeftSpacing,
$this->PAGE_HEIGHT - (self::VMARGIN + $this->vOffset), $textToAppend);
if ($textToAppend)
$this->current["char"] = $textToAppend{strlen($textToAppend)-1};
$this->current["char"] = $textToAppend[strlen($textToAppend)-1];

$this->hOffset += $this->currentPage->getTextWidth($textToAppend);

Expand Down Expand Up @@ -531,7 +531,7 @@ private function nextPage() {
} else {
$this->pages[$this->currentPageNumber] = $this->haruDoc->addPage();
$this->currentPage = $this->pages[$this->currentPageNumber];
$this->currentPage->setTextRenderingMode(\HaruPage::FILL);
$this->currentPage->setTextRenderingMode(Haru_HaruPage::FILL);
$this->vOffset = $this->currentFontSize;
$this->hOffset = ($this->hOffset ? $this->hOffset : 0);
$footerToAppend = true;
Expand Down Expand Up @@ -858,18 +858,18 @@ public function endTableRow() {
}

$this->currentPage->moveTo($x, $this->PAGE_HEIGHT - self::VMARGIN - ($this->current["vOffset"]));
$this->currentPage->lineTo($x, $this->PAGE_HEIGHT - self::VMARGIN - ($this->current["row"]["vPosition"] % $this->PAGE_HEIGHT));
$this->currentPage->lineTo($x, $this->PAGE_HEIGHT - self::VMARGIN - ((int)$this->current["row"]["vPosition"] % (int)$this->PAGE_HEIGHT));
$this->currentPage->stroke();
$this->current["vOffset"] = $vOffset;
}
// Horizontal line
$this->currentPage->moveTo(self::HMARGIN + $this->current["leftSpacing"], $this->PAGE_HEIGHT - self::VMARGIN - ($this->current["row"]["vPosition"] % $this->PAGE_HEIGHT));
$this->currentPage->moveTo(self::HMARGIN + $this->current["leftSpacing"], $this->PAGE_HEIGHT - self::VMARGIN - ((int)$this->current["row"]["vPosition"] % (int)$this->PAGE_HEIGHT));
$this->currentPage->lineTo($this->PAGE_WIDTH - self::HMARGIN - $this->current["rightSpacing"],
$this->PAGE_HEIGHT - self::VMARGIN - ($this->current["row"]["vPosition"] % $this->PAGE_HEIGHT));
$this->PAGE_HEIGHT - self::VMARGIN - ((int)$this->current["row"]["vPosition"] % (int)$this->PAGE_HEIGHT));
$this->currentPage->stroke();

// Store position
$this->vOffset = $this->current["row"]["vPosition"] % $this->PAGE_HEIGHT;
$this->vOffset = (int)$this->current["row"]["vPosition"] % (int)$this->PAGE_HEIGHT;

// Store pages
$last = array_pop($this->old);
Expand Down
22 changes: 22 additions & 0 deletions phpdotnet/phd/Haru/HaruAnnotation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace phpdotnet\phd;

class Haru_HaruAnnotation {
private $h = null;
private $ffi = null;

public function __construct($annotation_ref) {
$this->ffi = \FFI::load(__DIR__.'/hpdf.h');
$this->h = $annotation_ref;
if(is_null($this->h)) {
throw new Haru_HaruException('Cannot create HaruAnnotation handle');
}
}
public function setBorderStyle($width, $dash_on, $dash_off) {
$status = $this->ffi->HPDF_LinkAnnot_SetBorderStyle($this->h, $width, $dash_on, $dash_off);
if($status) {
throw new Haru_HaruException('', $status);
}
}
}
23 changes: 23 additions & 0 deletions phpdotnet/phd/Haru/HaruDestination.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace phpdotnet\phd;

class Haru_HaruDestination {
public $h = null;
private $ffi = null;

public function __construct($dest_ref) {
$this->ffi = \FFI::load(__DIR__.'/hpdf.h');
$this->h = $dest_ref;
if(is_null($this->h)) {
throw new Haru_HaruException('Cannot create HaruDestination handle');
}
}

public function setXYZ($left, $top, $zoom) {
$status = $this->ffi->HPDF_Destination_SetXYZ($this->h, $left, $top, $zoom);
if($status) {
throw new Haru_HaruException('', $status);
}
}
}
101 changes: 101 additions & 0 deletions phpdotnet/phd/Haru/HaruDoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace phpdotnet\phd;

class Haru_HaruDoc {
const COMP_ALL = 0x0F;
const PAGE_MODE_USE_OUTLINE = 1;

private $h = null;
private $ffi = null;

public function __construct() {
try {
$this->ffi = \FFI::load(__DIR__.'/hpdf.h');
} catch(\FFI\Exception $e) {
die($e->getFile() . ":" . $e->getLine() . ": " . $e->getMessage() . ". Is libharu installed?\n");
}
$this->h = $this->ffi->HPDF_New(null, null);
if(is_null($this->h)) {
throw new Haru_HaruException('Cannot create HaruDoc handle');
}
}

public function setCompressionMode($mode) {
$status = $this->ffi->HPDF_SetCompressionMode($this->h, $mode);
if($status) {
throw new Haru_HaruException('', $status);
}
}

public function addPageLabel($first_page, $style, $first_num, $string_prefix = null) {
$status = $this->ffi->HPDF_AddPageLabel($this->h, $first_page, $style, $first_num, $string_prefix);
if($status) {
throw new Haru_HaruException('', $status);
}
}

public function setPageMode($mode) {
$status = $this->ffi->HPDF_SetPageMode($this->h, $mode);
if($status) {
throw new Haru_HaruException('', $status);
}
}

public function setPagesConfiguration($page_per_pages) {
$status = $this->ffi->HPDF_SetPagesConfiguration($this->h, $page_per_pages);
if($status) {
throw new Haru_HaruException('', $status);
}
}

public function getFont($font_name, $encoding_name) {
$font_ref = $this->ffi->HPDF_GetFont($this->h, $font_name, $encoding_name);
if(is_null($font_ref)) {
throw new Haru_HaruException('Cannot create HaruFont handle');
}
$font = new Haru_HaruFont($font_ref);
return $font;
}

public function save($file_name) {
$status = $this->ffi->HPDF_SaveToFile($this->h, $file_name);
if($status) {
throw new Haru_HaruException('', $status);
}
}

// Note that the order of params differ from the underlying function
public function createOutline($title, $parent = null, $encoder = null) {
if($parent){
$parent = $parent->h;
}
$outline_ref = $this->ffi->HPDF_CreateOutline($this->h, $parent, $title, $encoder);
if(is_null($outline_ref)) {
throw new Haru_HaruException('Cannot create HaruOutline handle');
}
$outline = new Haru_HaruOutline($outline_ref);
return $outline;
}

public function addPage() {
$page_ref = $this->ffi->HPDF_AddPage($this->h);
if(is_null($page_ref)) {
throw new Haru_HaruException('Cannot create HaruPage handle');
}
$page = new Haru_HaruPage($page_ref);
return $page;
}

// TODO: this should return a new Haru_HaruImage instance
public function loadPNG($filename) {
$image_ref = $this->ffi->HPDF_LoadPngImageFromFile($this->h, $filename);
return $image_ref;
}

// TODO: this should return a new Haru_HaruImage instance
public function loadJPEG() {
$image_ref = $this->ffi->HPDF_LoadJpegImageFromFile($this->h, $filename);
return $image_ref;
}
}
15 changes: 15 additions & 0 deletions phpdotnet/phd/Haru/HaruException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace phpdotnet\phd;

class Haru_HaruException extends \Exception {
public function __construct($message, $code = 0, Throwable $previous = null) {
switch($code) {
default:
if(!$message) {
$message = "Error: libharu returned " . $code;
}
}
parent::__construct($message, $code, $previous);
}
}
28 changes: 28 additions & 0 deletions phpdotnet/phd/Haru/HaruFont.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace phpdotnet\phd;

class Haru_HaruFont {
public $h = null;
private $ffi = null;

public function __construct($font_ref) {
$this->ffi = \FFI::load(__DIR__.'/hpdf.h');
$this->h = $font_ref;
if(is_null($this->h)) {
throw new Haru_HaruException('Cannot create HaruFont handle');
}
}

public function measureText($text, $width, $font_size, $char_space, $word_space, $word_wrap = 0) {
$len = strlen($text);
if(!$len) {
return 0;
}
$str_ref_type = \FFI::arrayType($this->ffi->type("HPDF_BYTE"), [$len]);
$str_ref = $this->ffi->new($str_ref_type);
\FFI::memcpy($str_ref, $text, $len);
$fit_count = $this->ffi->HPDF_Font_MeasureText($this->h, $str_ref, $len, $width, $font_size, $char_space, $word_space, $word_wrap, null);
return (int)$fit_count;
}
}
31 changes: 31 additions & 0 deletions phpdotnet/phd/Haru/HaruOutline.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace phpdotnet\phd;

class Haru_HaruOutline {
public $h = null;
private $ffi = null;

public function __construct($outline_ref) {
$this->ffi = \FFI::load(__DIR__.'/hpdf.h');
$this->h = $outline_ref;
if(is_null($this->h)) {
throw new Haru_HaruException('Cannot create HaruOutline handle');
}
}

public function setDestination($dest) {
$status = $this->ffi->HPDF_Outline_SetDestination($this->h, $dest->h);
if($status) {
throw new Haru_HaruException('', $status);
}
}

public function setOpened($opened)
{
$status = $this->ffi->HPDF_Outline_SetOpened($this->h, $opened);
if($status) {
throw new Haru_HaruException('', $status);
}
}
}
Loading