-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdf_viewer.php
64 lines (52 loc) · 1.92 KB
/
pdf_viewer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
<?php
@header("Access-Control-Allow-Origin: *");
require 'vendor/autoload.php';
use Safe\Exceptions\FilesystemException;
use TheCodingMachine\Gotenberg\Client;
use TheCodingMachine\Gotenberg\DocumentFactory;
use TheCodingMachine\Gotenberg\OfficeRequest;
function create_pdf($url) {
$client_path = 'http://gotenberg:3000';
try {
$file_string = get_file_string_from_url($url);
} catch (Exception $e) {
echo '1Wystąpił wyjątek nr '.$e->getCode().', jego komunikat to:'.$e->getMessage();
}
$client = new Client($client_path, new \Http\Adapter\Guzzle6\Client());
try {
$files = [
DocumentFactory::makeFromString('test.doc', $file_string),
];
} catch (FilesystemException $e) {
echo '2Wystąpił wyjątek nr '.$e->getCode().', jego komunikat to:'.$e->getMessage();
}
try {
$request = new OfficeRequest($files);
$request->setWaitTimeout(20);
$response = $client->get_response($request);
$fileStream = $response->getBody()->getContents();
$client->post($request);
return $fileStream;
} catch (Exception $e) {
echo '<br>'.'3Wystąpił wyjątek nr '.$e->getCode().', jego komunikat to:'.$e->getMessage();
}
return '-1';
}
function get_file_string_from_url($url) {
$file_string = file_get_contents($url);
if($file_string === false){
throw new Exception('Failed to download file at: ' . $url);
}
return $file_string;
}
/**
* SET url which you want to be passed
*
* pattern
* http://mypage.com?url_address=http://secondpage.com/att.pdf
*/
if(isset($_GET['url_address'])){
$url = urldecode($_GET['url_address']);
echo create_pdf($url);
}
?>