forked from Muhimbi/PDF-Converter-Services-Online
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pdf_watermark.php
89 lines (71 loc) · 5.09 KB
/
pdf_watermark.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
// ** Make sure all objects associated with the Muhimbi client can be resolved
require_once(__DIR__ . '../../../../client/autoload.php');
// !!!! ENTER YOUR API KEY HERE !!!!
$api_key = '';
//** Was a file uploaded?
if($_FILES["file"]['size'] > 0)
{
//** Make sure an api key has been entered
if($api_key == '') {
echo 'Please update the sample code and enter the API Key that came with your subscription.';
exit;
}
// ** Specify the API key associated with your subscription.
MuhimbiPDFOnline\Client\Configuration::getDefaultConfiguration()->setApiKey('api_key', $api_key);
// ** The service's host name is already set, but for debugging purposes you may want to switch between 'http' and 'https'.'
MuhimbiPDFOnline\Client\Configuration::getDefaultConfiguration()->setHost('https://api.muhimbi.com/api');
// ** We are dealing with watermarking, so instantiate the relevant class
$api_instance = new MuhimbiPDFOnline\Client\Api\WatermarkApi();
// ** We need to fill out the data for the watermark operation
$input_data = new MuhimbiPDFOnline\Client\Model\PdfWatermarkData();
// ** Always pass the name of the input file, or if unknown pass any name, but with the correct file extension.
$input_data->setSourceFileName($_FILES["file"]["name"]);
// ** Pass the content of the uploaded file, making sure it is base64 encoded.
$input_data->setSourceFileContent(base64_encode(file_get_contents($_FILES["file"]["tmp_name"])));
// ** The PDF to use in the watermark, either a full URL or base64 encoded file. (The first page in the PDF will be used as the watermark)
$input_data->setPdfFile('http://www.muhimbi.com/Images---Files/PDFConverter/PDF-Converter-Brochure.aspx');
// ** Set the various properties associated with the watermark
$input_data->setPosition($input_data::POSITION_MIDDLE_CENTER); // ** The location where the watermark will be positioned on the page.
//$input_data->setX(0); // ** The x-coordinate of the watermark. Only used when Position is set to Absolute.
//$input_data->setY(0); // ** The y-coordinate of the watermark. Only used when Position is set to Absolute.
$input_data->setWidth(200); // ** The width of the watermark in pts (1/72nd of an inch). For example 144 is 2 inches wide.
$input_data->setHeight(286); // ** The height of the watermark.
$input_data->setLayer($input_data::LAYER_FOREGROUND); // ** Position the watermark in front of the page's content, or behind it
$input_data->setRotation(0); // ** Rotate the watermark, specify a value in degrees. Negative values are accepted.
$input_data->setOpacity(50); // ** A value between 0 (completely transparent) and 100 (fully visible)
//$input_data->setFillColor("#990000"); // ** The fill color of the watermark in #RRGGBB notation.
//$input_data->setLineColor("#000000"); // ** The (out)line color of the watermark in #RRGGBB notation.
//$input_data->setLineWidth(10); // ** The width of the line in pts (1/72nd of an inch). 0 = hairline, -1 = no line
$input_data->setStartPage(0); // ** The first page in the document the watermark applies to.
$input_data->setEndPage(0); // ** The last page in the document the watermark applies to.
$input_data->setPageInterval(1); // ** Which pages to apply the watermark on (1 = every page, 2 = every other page etc).
$input_data->setPageOrientation($input_data::PAGE_ORIENTATION_BOTH); // ** The page orientation the watermark applies to.
//** If you are expecting long running operations then consider longer timeouts
//** Also keep an eye on the maximum upload size in your php.ini (e.g. post_max_size = 10M, upload_max_filesize = 10M)
ini_set('default_socket_timeout', 300);
set_time_limit ( 300 );
try {
// ** Carry out the watermark operation
$result = $api_instance->pdfWatermark($input_data);
// ** Send the watermarked file back to the user
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"watermarked.pdf\"");
echo base64_decode($result->getProcessedFileContent());
exit;
} catch (Exception $e) {
echo 'Exception when calling API: ', $e->getMessage(), PHP_EOL;
}
}
?>
<html>
<body>
<form action="pdf_watermark.php" method="post" enctype="multipart/form-data">
<p>Please specify the PDF file to add a watermark to.</p>
<input type="file" name="file" />
<br/><br/>
<input type="submit" value="Watermark" />
</form>
</body>
</html>