Skip to content

Commit

Permalink
Finish implementation of PdfToText
Browse files Browse the repository at this point in the history
Add ability to specify sub directory for output
Add option for bounding box flag for poppler binaries
  • Loading branch information
Sam Koucha committed Oct 16, 2018
1 parent 27f912c commit 5532759
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 7 deletions.
3 changes: 3 additions & 0 deletions src/PopplerPhp/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ abstract class Constants
const _ICC = '-icc';
const _LEVEL2 = '-level2';
const _LEVEL3 = '-level3';
const _BBOX_LAYOUT = '-bbox-layout';
const _LAYOUT = '-layout';
const _BOX = '-box';

//Poppler Option DataTypes
const T_STRING = 'string';
Expand Down
3 changes: 3 additions & 0 deletions src/PopplerPhp/PdfInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use NcJoes\PopplerPhp\PopplerOptions\CredentialOptions;
use NcJoes\PopplerPhp\PopplerOptions\DateFlags;
use NcJoes\PopplerPhp\PopplerOptions\EncodingOptions;
use NcJoes\PopplerPhp\PopplerOptions\InfoFlags;
use NcJoes\PopplerPhp\PopplerOptions\PageRangeOptions;

class PdfInfo extends PopplerUtil
Expand All @@ -23,6 +24,7 @@ class PdfInfo extends PopplerUtil
use EncodingOptions;
use PageRangeOptions;
use ConsoleFlags;
use InfoFlags;

private $pdf_info;

Expand Down Expand Up @@ -211,6 +213,7 @@ public function utilOptionRules()
public function utilFlags()
{
return array_merge(
$this->infoFlags(),
$this->dateFlags(),
$this->encodingFlags(),
$this->allConsoleFlags()
Expand Down
44 changes: 40 additions & 4 deletions src/PopplerPhp/PdfToText.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,66 @@

namespace NcJoes\PopplerPhp;

use NcJoes\PopplerPhp\Constants as C;
use NcJoes\PopplerPhp\PopplerOptions\ConsoleFlags;
use NcJoes\PopplerPhp\PopplerOptions\CredentialOptions;
use NcJoes\PopplerPhp\PopplerOptions\EncodingOptions;
use NcJoes\PopplerPhp\PopplerOptions\HtmlOptions;
use NcJoes\PopplerPhp\PopplerOptions\PageRangeOptions;
use NcJoes\PopplerPhp\PopplerOptions\TextFlags;

class PdfToText extends PopplerUtil
{
use PageRangeOptions;
use ConsoleFlags;
use HtmlOptions;
use EncodingOptions;
use CredentialOptions;
use TextFlags;


public function __construct($pdfFile = '', array $options = [])
{
$this->bin_file = C::PDF_TO_TEXT;
return parent::__construct($pdfFile, $options);
}

public function utilOptions()
{
// TODO: Implement utilOptions() method.
return array_merge(
$this->pageRangeOptions(),
$this->htmlOptions(),
$this->credentialOptions(),
$this->encodingOptions()
);
}

public function utilOptionRules()
{
// TODO: Implement utilOptionRules() method.
return [
'alt' => [],
];
}

public function utilFlags()
{
// TODO: Implement utilFlags() method.
return $this->textFlags();
}

public function utilFlagRules()
{
// TODO: Implement utilFlagRules() method.
return [
'alt' => [],
];
}

public function outputExtension()
{
return '.txt';
}

public function generate()
{
return $this->shellExec();
}
}
18 changes: 18 additions & 0 deletions src/PopplerPhp/PopplerOptions/InfoFlags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace NcJoes\PopplerPhp\PopplerOptions;

use NcJoes\PopplerPhp\Constants as C;

trait InfoFlags
{
public function setBox()
{
return $this->setFlag(C::_BOX);
}

protected function infoFlags()
{
return [C::_BOX];
}
}
23 changes: 23 additions & 0 deletions src/PopplerPhp/PopplerOptions/TextFlags.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace NcJoes\PopplerPhp\PopplerOptions;

use NcJoes\PopplerPhp\Constants as C;

trait TextFlags
{
public function setBboxLayout()
{
return $this->setFlag(C::_BBOX_LAYOUT);
}

public function setLayout()
{
return $this->setFlag(C::_LAYOUT);
}

protected function textFlags()
{
return [C::_BBOX_LAYOUT, C::_LAYOUT];
}
}
38 changes: 35 additions & 3 deletions src/PopplerPhp/PopplerUtil.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,21 @@ abstract class PopplerUtil
{
protected $bin_file;
protected $output_file_extension;
protected $require_output_dir = true;
protected $require_output_dir = true;
protected $require_sub_dir = true;
private $binary_dir;
private $flags = [];
private $options = [];
private $source_pdf;
private $output_sub_dir;
private $output_file_name;


/**
* PopplerUtil constructor.
*
* @param string $pdfFile
* @param array $options
* @throws PopplerPhpException
*/
public function __construct($pdfFile = '', array $options = [])
{
Expand Down Expand Up @@ -110,7 +112,7 @@ public function getOutputSubDir()
*/
public function getOutputPath()
{
return Config::getOutputDirectory().C::DS.$this->getOutputSubDir();
return Config::getOutputDirectory() . C::DS . ($this->isSubDirRequired() ? $this->getOutputSubDir() : '') ;
}

/**
Expand Down Expand Up @@ -394,6 +396,36 @@ public function getOutputFilenamePrefix()
return $default_name;
}

/**
* @param bool $bool
* @return $this
*/
public function setRequireOutputDir($bool)
{
$this->require_output_dir = $bool;

return $this;
}

/**
* @return bool
*/
public function isSubDirRequired()
{
return $this->output_sub_dir;
}

/**
* @param bool $bool
* @return $this
*/
public function setSubDirRequired ($bool)
{
$this->output_sub_dir = $bool;

return $this;
}

/**
* @return mixed
*/
Expand Down

0 comments on commit 5532759

Please sign in to comment.