minor updates

parent 904e71ee
......@@ -23,56 +23,47 @@ use Drupal\Core\TypedData\Plugin\DataType\TimeStamp;
use Drupal\Core\Language\LanguageManagerInterface;
//require_once('fpdf.php');
//require('C:\wamp64\bin\php\php5.6.25\fpdf16/fpdf.php');
use FPDF;
class PDFCreator extends ControllerBase {
protected $entity_query;
protected $entityTypeManager;
protected $logger;
protected $connection;
protected $entityTypeManager;
protected $logger;
protected $connection;
public function __construct(
EntityTypeManagerInterface $entityTypeManager,
QueryFactory $entity_query,
Connection $connection,
LoggerChannelFactoryInterface $loggerChannel)
{
$this->entityTypeManager = $entityTypeManager;
$this->entity_query = $entity_query;
$connection = Database::getConnection();
$this->connection = $connection;
$this->logger = $loggerChannel->get('epal');
{
$this->entityTypeManager = $entityTypeManager;
$this->entity_query = $entity_query;
$connection = Database::getConnection();
$this->connection = $connection;
$this->logger = $loggerChannel->get('epal');
}
public static function create(ContainerInterface $container)
{
return new static(
$container->get('entity_type.manager'),
$container->get('entity.query'),
$container->get('database'),
$container->get('logger.factory')
$container->get('entity_type.manager'),
$container->get('entity.query'),
$container->get('database'),
$container->get('logger.factory')
);
}
public function createApplicantPDF(Request $request) {
try {
if (!$request->isMethod('GET')) {
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
return $this->respondWithStatus([
"message" => t("Method Not Allowed")
], Response::HTTP_METHOD_NOT_ALLOWED);
}
/*
$authToken = $request->headers->get('PHP_AUTH_USER');
$epalUsers = $this->entityTypeManager->getStorage('epal_users')->loadByProperties(array('authtoken' => $authToken));
......@@ -89,7 +80,6 @@ class PDFCreator extends ControllerBase {
}
*/
//user validation
/*
$authToken = $request->headers->get('PHP_AUTH_USER');
......@@ -130,75 +120,74 @@ class PDFCreator extends ControllerBase {
], Response::HTTP_FORBIDDEN);
}
*/
//$user->id()
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',16);
$pdf->Cell(40,10,'Hello World! This is a funny day!!!!Hello World! This is a funny day!!!!Hello World! This is a funny day!!!!');
ob_end_clean();
//$s = $pdf->Output("c:\BACKUP\myFile2.pdf", "D");
$s = $pdf->Output("c:\BACKUP\myFile2.pdf", "S");
$s = utf8_encode($s);
$pdf->Close();
//ob_end_flush();
//$s = file_get_contents("c:/BACKUP/test.pdf");
//$s = utf8_encode($s);
//file_put_contents("c:/BACKUP/test2.pdf",$s);
return $this->respondWithStatus([
'pdfString' => $s,
//$s,
], Response::HTTP_OK);
} //end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return $this->respondWithStatus([
"message" => t("An unexpected problem occured during createApplicantPDF Method ")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage();
//Mine
//$pdf->SetFont('Arial','B',16);
//$pdf->Cell(40,10,'Hello World! This is a funny day!!!!Hello World! This is a funny day!!!!Hello World! This is a funny day!!!!');
$pdf->AddFont('open-sans.light', '', 'open-sans.light.php');
$pdf->SetFont('open-sans.light', '', 16);
$pdf->Cell(40,10, $this->prepareString('Καλό Καλοκαίρι!!!'));
//End Mine
/*
// $pdf->SetFont('Arial','B',16);
$pdf->AddFont('Ubuntu', '', 'Ubuntu-Regular.php');
$pdf->SetFont('Ubuntu', '', 16);
$pdf->Cell(40, 10, $this->prepareString('Tούτη εδώ είναι η αίτηση σου'));
$pdf->Ln(10);
$pdf->Cell(40, 10, $this->prepareString('Χρονοσήμανση: ') . date('d/m/Y H:i:s'));
$pdf->Ln(10);
$pdf->SetFont('Ubuntu','',10);
for ($i=1; $i<=30; $i++) {
$pdf->Cell(0, 8 , $this->prepareString('Στοιχείο: ') . $i, 0, 1);
$pdf->Cell(0, 8 , 'Item: ' . $i, 0, 1);
}
*/
$pdf->Close(); // Δεν χρειάζεται, το κάνει η Output
//$s = $pdf->Output("S", "export.pdf", true);
$s = $pdf->Output("S", "export.pdf", true);
}
$response = new Response($s, Response::HTTP_OK, ['Content-Type', 'application/pdf']);
return $response;
public function array_utf8_encode($dat)
{
if (is_string($dat))
return utf8_encode($dat);
if (!is_array($dat))
return $dat;
$ret = array();
foreach ($dat as $i => $d)
$ret[$i] = self::array_utf8_encode($d);
return $ret;
} //end try
catch (\Exception $e) {
$this->logger->warning($e->getMessage());
return $this->respondWithStatus([
"message" => t("An unexpected problem occured during createApplicantPDF Method ")
], Response::HTTP_INTERNAL_SERVER_ERROR);
}
private function respondWithStatus($arr, $s) {
$res = new JsonResponse($arr);
$res->setStatusCode($s);
return $res;
}
}
private function prepareString($string, $from_encoding = 'UTF-8', $to_encoding = 'ISO-8859-7') {
return iconv($from_encoding, $to_encoding, $string);
}
// OBSOLETE
// public function array_utf8_encode($dat)
// {
// if (is_string($dat))
// return utf8_encode($dat);
// if (!is_array($dat))
// return $dat;
// $ret = array();
// foreach ($dat as $i => $d)
// $ret[$i] = self::array_utf8_encode($d);
// return $ret;
// }
private function respondWithStatus($arr, $s) {
$res = new JsonResponse($arr);
$res->setStatusCode($s);
return $res;
}
}
......@@ -102,6 +102,7 @@ import {
<br>
<button type="button" (click)="createPdfServerSide()">Εξαγωγή σε PDF</button>
<!--
<form [formGroup]="formGroup" method = "POST" action="{{apiEndPoint}}/drupal-8.2.6/epal/pdf-application" #form>
<button type="submit" (click)="form.submit()">Εξαγωγή σε PDF - ΝΕΟ!</button>
......
......@@ -57,6 +57,9 @@ export class HelperDataService implements OnInit, OnDestroy {
}
createAuthorizationHeader(headers: Headers) {
console.log("Αυτά είναι Α:");
console.log(this.authToken);
console.log(this.authRole);
headers.append('Authorization', 'Basic ' + btoa(this.authToken + ':' + this.authToken));
}
......@@ -920,7 +923,7 @@ export class HelperDataService implements OnInit, OnDestroy {
}
createPdfServerSide(auth_token, role) {
createPdfServerSide_OLD(auth_token, role) {
/*
this.loginInfo$.getValue().forEach(loginInfoToken => {
......@@ -937,13 +940,24 @@ createPdfServerSide(auth_token, role) {
//"Access-Control-Allow-Origin": "true",
});
this.authToken = auth_token;
this.authRole = role;
//this.authToken = auth_token;
//this.authRole = role;
console.log("Αυτά είναι:");
console.log(this.authToken);
console.log(this.authRole);
this.createAuthorizationHeader(headers);
let options = new RequestOptions([{ headers: headers }, { responseType: ResponseContentType.Blob }]);
//let options = new RequestOptions({ headers: headers });
//let options = new RequestOptions([{ headers: headers }, { responseType: ResponseContentType.Blob }]);
let options = new RequestOptions({ headers: headers });
console.log("Nikos1");
//return this.http.get(`${AppSettings.API_ENDPOINT}/epal/pdf-application/`, options)
// .map(response => response.json());
return this.http.get(`${AppSettings.API_ENDPOINT}/epal/pdf-application/`, options)
.map(response => response.json())
......@@ -965,6 +979,7 @@ createPdfServerSide(auth_token, role) {
);
/*
return new Promise((resolve, reject) => {
this.http.post(`${AppSettings.API_ENDPOINT}/epal/pdf-application`, options)
......@@ -989,4 +1004,89 @@ createPdfServerSide(auth_token, role) {
}
createPdfServerSide(auth_token, role) {
/*
this.loginInfo$.getValue().forEach(loginInfoToken => {
this.authToken = loginInfoToken.auth_token;
this.authRole = loginInfoToken.auth_role;
console.log("Θα μπει;");
console.log(this.authToken);
});
*/
let headers = new Headers({
"Content-Type": "application/json",
//"Access-Control-Allow-Origin": "true",
});
this.authToken = auth_token;
this.authRole = role;
// console.log(this.authToken);
// console.log(this.authRole);
this.createAuthorizationHeader(headers);
// let options = new RequestOptions([{ headers: headers }, { responseType: ResponseContentType.ArrayBuffer }]);
let options = new RequestOptions({ headers: headers, responseType: ResponseContentType.Blob });
return this.http.get(`${AppSettings.API_ENDPOINT}/epal/pdf-application/`, options)
// .map(response => response.json())
// .map((res: Response): Blob => {
// return (<any>res)._body;
// })
// .map((res: Response): Blob => {
// return res.ok ?
// // res.blob()
// new Blob([(<any>res)._body], { type: 'application/octet-stream' })
// : undefined;
// })
// .map(res => res.blob())
//.map(response => (<Response>response).blob())
.map( (res) => {
return new Blob([res['_body']], {type: "application/octet-stream" })
})
.subscribe(
data => {
console.log(data);
// var blob = new Blob([data['pdfString']], { type: 'application/octet-stream' });
// var blob = new Blob([data.arrayBuffer()], { type: 'application/octet-stream' });
// var blob = new Blob([data.blob()], { type: 'application/pdf' });
// var blob = new Blob([data], { type: 'application/pdf' });
// var blob = new Blob([btoa(data['pdfString'])]);
// console.log(blob);
FileSaver.saveAs(data, "testData.pdf");
// var url = window.URL.createObjectURL(blob);
// window.open(url);
},
err => console.error(err),
() => console.log('PDF export completed')
);
/*
return new Promise((resolve, reject) => {
this.http.post(`${AppSettings.API_ENDPOINT}/epal/pdf-application`, options)
//.map(response => response.json())
.subscribe(data => {
resolve(data);
console.log("Nik");
var blob = new Blob([data['_body']], {type: 'application/pdf'});
console.log(blob);
FileSaver.saveAs(blob, "testData.pdf");
//console.log(data['_body']);
},
error => {
reject("Error POST in createPdfServerSide");
},
() => console.log("Nikos!!!"));
});
*/
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment