Commit f8c046e9 authored by Florian Azizian's avatar Florian Azizian

FEAT #9478 add pdf library and php qrcode in composer + get system tp dir

parent ec78a902
Pipeline #2849 failed with stage
in 2 minutes and 45 seconds
{
"require": {
"khanamiryan/qrcode-detector-decoder": "^1.0",
"setasign/fpdi-tcpdf": "^2.0.0",
"setasign/fpdf": "^1.8.1",
"setasign/fpdi-fpdf": "^2.0.0",
"setasign/fpdi-tfpdf": "^2.0.0"
}
}
<?php
class QRSeparator
class QRSeparator
{
function __construct()
public function __construct()
{
$this->Batch = $_SESSION['capture']->Batch;
require __DIR__ . "/../../vendor/autoload.php";
}
function separatePDF($ScanSource,$ResultDirectory = false)
public function separatePDF($ScanSource, $ResultDirectory = false)
{
echo "Init process ...\n";
$_SESSION['capture']->logEvent(
"Init process ... "
......@@ -60,7 +59,7 @@ class QRSeparator
//call split function to sepearate pages
try {
$this->split_pdf($ScanSource.$files[$key], realpath('tmp').'/'.$key);
$this->split_pdf($ScanSource.$files[$key], sys_get_temp_dir().'/'.$key);
} catch (Exception $e) {
echo 'ERROR (move '.$files[$key].' to '.$ScanSource.'FAILED/) ! ', $e->getMessage(), "\n";
$_SESSION['capture']->logEvent(
......@@ -73,9 +72,9 @@ class QRSeparator
copy($ScanSource.$files[$key], $ScanSource.'FAILED/'.$files[$key]);
}
//merge pages previously splited
$this->construct_pdf(realpath('tmp').'/'.$key, $ResultDirectory);
$this->construct_pdf(sys_get_temp_dir().'/'.$key, $ResultDirectory);
unlink($ScanSource.$files[$key]);
rmdir(realpath(realpath('tmp').'/'.$key));
rmdir(realpath(sys_get_temp_dir().'/'.$key));
} else {
echo "No pdf format ! skipping ...\n";
$_SESSION['capture']->logEvent(
......@@ -85,10 +84,11 @@ class QRSeparator
$num_file++;
}
$files = glob(realpath('tmp').'/*'); // get all file names
foreach($files as $file){ // iterate files
if(is_file($file))
unlink($file); // delete file
$files = glob(sys_get_temp_dir().'/*'); // get all file names
foreach ($files as $file) { // iterate files
if (is_file($file)) {
unlink($file);
} // delete file
}
echo "End of process ...\n";
......@@ -97,11 +97,8 @@ class QRSeparator
);
}
function split_pdf($filename, $end_directory)
public function split_pdf($filename, $end_directory)
{
require_once 'tcpdf/tcpdf.php';
require_once 'tcpdf/tcpdi.php';
$end_directory = $end_directory.'/';
/*
......@@ -113,19 +110,19 @@ class QRSeparator
mkdir($end_directory, 0755, true);
}
$pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$pdf = new \setasign\Fpdi\Fpdi('P', 'mm');
//How manu pages ?
$pdfdata = file_get_contents($filename);
$pagecount = $pdf->setSourceData($pdfdata);
$pagecount = $pdf->setSourceFile($filename);
//Split each page of pdf file
for ($i = 1; $i <= $pagecount; $i++) {
$new_pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$new_pdf = new \setasign\Fpdi\Fpdi('P', 'mm');
$new_pdf->AddPage();
$new_pdf->setSourceData($pdfdata);
$new_pdf->setSourceFile($filename);
$tplidx = $new_pdf->importPage($i);
$new_pdf->useTemplate($tplidx);
......@@ -147,16 +144,13 @@ class QRSeparator
}
}
function construct_pdf($split_directory, $end_directory = false)
public function construct_pdf($split_directory, $end_directory = false)
{
require_once 'tcpdf/tcpdf.php';
require_once 'tcpdf/tcpdi.php';
include_once 'qrReader/QrReader.php';
$end_directory = $end_directory ? $end_directory : realpath('tmp').'/';
$end_directory = $end_directory ? $end_directory : sys_get_temp_dir().'/';
//$new_pdf = new FPDI();
$new_pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$new_pdf = new \setasign\Fpdi\Fpdi('P', 'mm');
$i=1;
$z=0;
......@@ -166,12 +160,11 @@ class QRSeparator
while ($i!=0) {
if (is_file($split_directory.$i.'.pdf')) {
$file = $i.'.pdf';
$next_filename = $i+1;
//Attempt to extract QRCODE
$qrcode = new QrReader($split_directory.$file);
$qrcode = new \Zxing\QrReader($split_directory.$file);
$pdfdata = file_get_contents($split_directory.$file);
$text = $qrcode->text();
......@@ -186,8 +179,7 @@ class QRSeparator
$old_label = $qr_label;
$qr_label = $text;
$label[]=$text;
} else if (empty($text) && !empty($qr_label)) {
} elseif (empty($text) && !empty($qr_label)) {
//If not a separator, merge of each previous pdf
$isCourrier =true;
echo "This is a maarch document page ! Add page to pdf ...";
......@@ -197,11 +189,10 @@ class QRSeparator
);
$new_pdf->AddPage();
$new_pdf->setSourceData($pdfdata);
$new_pdf->setSourceFile($split_directory.$file);
$tplidx = $new_pdf->importPage(1);
$new_pdf->useTemplate($tplidx);
} else {
// If not a separator and no previous pdf, merge actual pdf
echo "No separator found ! Add page to pdf ...";
......@@ -212,7 +203,7 @@ class QRSeparator
$label[$z] = 'NOSEPARATOR';
$new_pdf->AddPage();
$new_pdf->setSourceData($pdfdata);
$new_pdf->setSourceFile($split_directory.$file);
$tplidx = $new_pdf->importPage(1);
$new_pdf->useTemplate($tplidx);
......@@ -222,7 +213,6 @@ class QRSeparator
* Creation du pdf du separateur
*/
if ((($isCourrier == false && $old_label != '' && $text != '') || (!is_file($split_directory.$next_filename.'.pdf')))) {
//
echo "End of merge pdf for service : ".$label[$z];
echo "\n";
$_SESSION['capture']->logEvent(
......@@ -248,11 +238,11 @@ class QRSeparator
//add destination data
$Document->setMetadata(
"destination",
"destination",
$label[$z-1]
);
$new_pdf = new TCPDI(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
$new_pdf = new \setasign\Fpdi\Fpdi('P', 'mm');
}
echo "Remove tmp file : ".$split_directory.$i.'.pdf';
echo "\n";
......
<?php
include_once ('Reader.php');
require_once ('BinaryBitmap.php');
require_once ('common/detector/MathUtils.php');
require_once ('common/BitMatrix.php');
require_once ('common/BitSource.php');
require_once ('common/BitArray.php');
require_once ('BinaryBitmap.php');
include_once ('LuminanceSource.php');
include_once ('GDLuminanceSource.php');
include_once ('IMagickLuminanceSource.php');
include_once ('common/customFunctions.php');
include_once ('common/PerspectiveTransform.php');
include_once ('common/GridSampler.php');
include_once ('common/DefaultGridSampler.php');
include_once ('common/DetectorResult.php');
require_once ('common/reedsolomon/GenericGFPoly.php');
require_once ('common/reedsolomon/GenericGF.php');
include_once ('common/reedsolomon/ReedSolomonDecoder.php');
include_once ('common/reedsolomon/ReedSolomonException.php');
include_once ('qrcode/decoder/Decoder.php');
include_once ('ReaderException.php');
include_once ('NotFoundException.php');
include_once ('FormatException.php');
include_once ('ChecksumException.php');
include_once ('qrcode/detector/FinderPatternInfo.php');
include_once ('qrcode/detector/FinderPatternFinder.php');
include_once ('ResultPoint.php');
include_once ('qrcode/detector/FinderPattern.php');
include_once ('qrcode/detector/AlignmentPatternFinder.php');
include_once ('qrcode/detector/AlignmentPattern.php');
include_once ('qrcode/decoder/Version.php');
include_once ('qrcode/decoder/BitMatrixParser.php');
include_once ('qrcode/decoder/FormatInformation.php');
include_once ('qrcode/decoder/ErrorCorrectionLevel.php');
include_once ('qrcode/decoder/DataMask.php');
include_once ('qrcode/decoder/DataBlock.php');
include_once ('qrcode/decoder/DecodedBitStreamParser.php');
include_once ('qrcode/decoder/Mode.php');
include_once ('common/DecoderResult.php');
include_once ('Result.php');
include_once ('Binarizer.php');
include_once ('common/GlobalHistogramBinarizer.php');
include_once ('common/HybridBinarizer.php');
final class QrReader
{
public $result;
function __construct($filename)
{
try {
if(extension_loaded('imagick')) {
$im = new Imagick();
$im->readImage($filename);
$width = 300;
$height = 300;
$source = new \Zxing\IMagickLuminanceSource($im, $width, $height);
}else {
$image = file_get_contents($filename);
$sizes = getimagesize($filename);
$width = $sizes[0];
$height = $sizes[1];
$im = imagecreatefromstring($image);
$source = new \Zxing\GDLuminanceSource($im, $width, $height);
}
$histo = new \Zxing\Common\HybridBinarizer($source);
$bitmap = new \Zxing\BinaryBitmap($histo);
$reader = new \Zxing\Qrcode\QRCodeReader();
$this->result = $reader->decode($bitmap);
}catch (\Zxing\NotFoundException $er){
$this->result = false;
}catch( \Zxing\FormatException $er){
$this->result = false;
}catch( \Zxing\ChecksumException $er){
$this->result = false;
}
}
public function text()
{
if(method_exists($this->result,'toString')) {
return ($this->result->toString());
}else{
return $this->result;
}
}
public function decode()
{
return $this->text();
}
}
<?php
namespace Zxing;
require_once('qrcode/QRCodeReader.php');
interface Reader {
public function decode($image);
public function reset();
}
\ No newline at end of file
<?php
/*
* Copyright 2007 ZXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Zxing;
/**
* <p>Encapsulates the result of decoding a barcode within an image.</p>
*
* @author Sean Owen
*/
final class Result {
private $text;
private $rawBytes;
private $resultPoints;
private $format;
private $resultMetadata;
private $timestamp;
public function __construct($text,
$rawBytes,
$resultPoints,
$format,
$timestamp = '') {
$this->text = $text;
$this->rawBytes = $rawBytes;
$this->resultPoints = $resultPoints;
$this->format = $format;
$this->resultMetadata = null;
$this->timestamp = $timestamp?$timestamp:time();
}
/**
* @return raw text encoded by the barcode
*/
public function getText() {
return $this->text;
}
/**
* @return raw bytes encoded by the barcode, if applicable, otherwise {@code null}
*/
public function getRawBytes() {
return $this->rawBytes;
}
/**
* @return points related to the barcode in the image. These are typically points
* identifying finder patterns or the corners of the barcode. The exact meaning is
* specific to the type of barcode that was decoded.
*/
public function getResultPoints() {
return $this->resultPoints;
}
/**
* @return {@link BarcodeFormat} representing the format of the barcode that was decoded
*/
public function getBarcodeFormat() {
return $this->format;
}
/**
* @return {@link Map} mapping {@link ResultMetadataType} keys to values. May be
* {@code null}. This contains optional metadata about what was detected about the barcode,
* like orientation.
*/