Closed divsbhalala closed 6 years ago
Hello @divsbhalala , please can you paste your code to better understand where the problem can be.
Thanks,
@marcogrossisas here is the code
public function addWatermarkToPdf($pdfFilePath, $watermark_type='text', $watermark_text='', $watermark_image='', $fontSize=18, $fontColor='#000',
$fontFamily='courier', $range='all', $horizontalPosition='left', $verticalPosition='bottom', $opacity=50, $page_layer_pos='bottom', $watermark_rotation=35, $rotation=35, $name, $pkjFileName){
$watermark = new WatermarkTask( $this->publicKey, $this->secretKey );
$fileA = $watermark->addFile($pdfFilePath);
$fileA->setRotation($rotation);
if($watermark_type=='text'){
$watermark->setText($watermark_text);
$watermark->setFontFamily($fontFamily);
$watermark->setFontSize($fontSize);
$watermark->setFontColor($fontColor);
}
else if(!empty($watermark_image)){
$watermark->setImage($watermark_image);
}
$watermark->setMosaic(true);
$watermark->setPages($range);
$watermark->setHorizontalPosition($horizontalPosition);
$watermark->setVerticalPosition($verticalPosition);
$watermark->setTransparency($opacity);
$watermark->setLayer($page_layer_pos);
$watermark->setRotation($watermark_rotation);
$watermark->setOutputFilename($name);
$watermark->setPackagedFilename($pkjFileName);
$watermark->execute();
return $watermark;
}
and I am calling function like this
$ILovepdf_WPPlugin_Tasks->addWatermarkToPdf($upload['path'] . '/' . $fileName, $watermark_type, $watermark_text, $upload['path'] . '/' . $watermark_image, $fontSize, $fontColor, $fontFamily,
$fromPage.'-'.$toPage, $horizontalPosition, $verticalPosition, $opacity, $page_layer_pos, $watermark_rotation, $rotation, $outputFileName, $pkjFileName);
Hello divsbhalala,
As shown in samples, you need to add the image file like you do with the pdf file, and then set the result object as a watermark image.
This should be your code when adding image:
...
else if(!empty($watermark_image)){
$image = $watermark->addFile($watermark_image);
$watermark->setImage($watermark_image);
}
...
By the way, I have seen that you develop a wordpress plugin and I invite you to see ours at https://wordpress.org/plugins/ilovepdf/ (and code here https://github.com/ilovepdf/ilovepdf-wp), maybe will be useful, and/or save you some work...
@maztch I am trying to customize your WordPress plugins as per my need for the client side.
And I am still getting null response after adding code as per your suggestions the code looks like this http://prntscr.com/h2lmgo
and here are params that I am passing http://prntscr.com/h2lmpb (Note: Don't worry About the path I am passing full path)
Sorry, my fault:
... else if(!empty($watermark_image)){ $image = $watermark->addFile($watermark_image); $watermark->setImage($image); } ...
@maztch sorry but this solution is also not working
here is the code http://prntscr.com/h2ou3f and here is the request Params http://prntscr.com/h2ouij
is there any sample code? so I can see that
Is so strange because your dump files field only have the pdf file and not the image file (should contain both files). And watermark_image field should contain an id, not the filename. If it's from the code.
Add also the mode
$watermark->setMode('text') ; // or 'image'
Here you can find samples: https://github.com/ilovepdf/ilovepdf-php/tree/master/samples
Try to do a complete process for each mode (text, and image).
@maztch I am already passing image file http://prntscr.com/h2pw0l
I tried with both image and text mode but nothing works and in your sample there is not sample of add image to the watermark
@divsbhalala we are looking into it. Your code seems right if you added the following line to your last code sent to us
$watermark->setMode('image');
But with the code you sent us something is wrong with the request data is sending.
@marcogrossisas if you want to test the result Please look at https://www.tweak.com/add-watermark/ the code is pushed here as per your suggestion and it's still not working. perhaps for the text watermark, it's working fine.
so do you want me try other solution?
@divsbhalala sorry for the late answer, here there is a complete working example with an image. The problem may be on the suggested $watermark->setImage($image);
that it should have been $watermark->setImage($image->server_filename);
:
//include the autoloader
require_once('../vendor/autoload.php');
use Ilovepdf\WatermarkTask;
// you can call task class directly
// to get your key pair, please visit https://developer.ilovepdf.com/user/projects
$myTask = new WatermarkTask('public','secret')
// file var keeps info about server file id, name...
// it can be used latter to cancel file
$file = $myTask->addFile('pols.pdf');
$image = $myTask->addFile('img.png');
// set mode to text
$myTask->setMode("image");
// set the text
//$myTask->setText("watermark text");
$myTask->setImage($image->server_filename);
// set pages to apply the watermark
$myTask->setPages("1-5,7");
// set vertical position
$myTask->setVerticalPosition("top");
// set horizontal position
$myTask->setHorizontalPosition("right");
// adjust vertical position
$myTask->setVerticalPositionAdjustment("100");
// adjust horizontal position
$myTask->setHorizontalPositionAdjustment("100");
// set mode to text
$myTask->setFontFamily("Arial");
// set mode to text
$myTask->setFontStyle("Italic");
// set the font size
$myTask->setFontSize("12");
// set color to red
$myTask->setFontColor("#ff0000");
// set transparency
$myTask->setTransparency("50");
// set the layer
$myTask->setLayer("below");
// and set name for output file.
// the task will set the correct file extension for you.
$myTask->setOutputFilename('watermarked');
// process files
$myTask->execute();
// and finally download the unlocked file. If no path is set, it will be downloaded on current folder
$myTask->download();
Please inform us if now you could solve it, if so, we will update the code to accept $image as object or $image->server_filename to avoid this kind of errors.
@marcogrossisas thanks for your supports this solution is working $myTask->setImage($image->server_filename);
Also, there is some documentation issue from your site that I have just notice. If you want to know Please let me know where I put suggestion for that
Add image to Watermark is not working getting null results
Here is the ILovePDFwatermark object Ilovepdf\WatermarkTask Object ( [mode] => [text] => [image] => /var/www/tweak.com/htdocs/wp-content/uploads/2017/10/A-letter2-11.png [pages] => 1-3 [vertical_position] => bottom [verticalPositionValues:Ilovepdf\WatermarkTask:private] => Array ( [0] => bottom [1] => middle [2] => top )
)