PHPWord generated docx watermark image with 0 margin is shifted with some space from left and top edge of document - watermark

I generate a docx document with PHPWord and want to make an image to be as a background. Watermark is totally the point for it.
Everything works fine generating document like sections, paragraphs, font styles and sizes, line heights, etc.
But I stucked with this part: my image for background with margins 0 is always shifted with some space from the document's edge.
I go with this code:
$phpWord = new \PhpOffice\PhpWord\PhpWord();
$section = $phpWord->addSection();
$header = $section->addHeader();
$header->addWatermark($path . '/images/test.jpg',
array(
'marginTop' => 0,
'marginLeft'=> 0,
)
);
$section->addText('Is Richard Deckard a replicant?');
$timestamp = date('Hi-ymd');
$filename = 'DOC_' . $timestamp;
$objWriter = \PhpOffice\PhpWord\IOFactory::createWriter($phpWord, 'Word2007');
$objWriter->save($path . '/docs/' . $filename . '.docx');
The result is:
PHPWord watermark image for background
So as you see margins are not 0 but the image is some distance from the left and top edge of the page.

I was able positioning background image to the top-left edge this way
$header = $section->addHeader();
$header->addWatermark('images/test.png',
array(
'width' => 596,
'marginTop' => -36,
'marginLeft' => -50,
'posHorizontal' => 'absolute',
'posVertical' => 'absolute',
));
I think the point is the absolute parameter.
I needed to make negative values on margins, because my body section had inner margins.

I was having the same problem and fortunately I found this question and the answer from #vorvor. Thank you for the answer #vorvor.
I did the same thing in a Laravel 5.6 project and just wanted to share my code with you.
$header = $section->addHeader();
$bg_image = Storage::url('itinerary-assets/Untitled-1_compressed.jpg');
$header->addWatermark($bg_image, [
'marginTop' => -36,
'marginLeft' => -50,
'posHorizontal' => 'absolute',
'posVertical' => 'absolute'
]);

Related

How can I increase the maximum file name size of attachments in Prestashop 1.7

On Prestashop 1.7, how can I increase the maximum file name size of attachments?
While naming attachment files in the Back Office, I encounter the following error message.
How can I go around this problem in Prestashop 1.7? I looked up online and found some solutions for Prestashop 1.5 or 1.6, but not for 1.7.
Here are some links that I followed:
https://www.prestashop.com/forums/topic/63332-bug-the-field-mime-is-too-long-32-chars-max/?tab=comments#comment-1189602
https://www.prestashop.com/forums/topic/81129-solvedhow-to-change-the-length-of-the-file-name-of-the-attachement/?tab=comments#comment-1197114
Following the above links, I have tried:
In the DB, changed the 'name' column's type in the 'ps_attachment_lang' table to 'varchar(128)'.
In Attachment.php (/classes/Attachment.php), made sure the size is 128.
'name' => ['type' => self::TYPE_STRING, 'lang' => true, 'validate' => 'isGenericName', 'required' => true, 'size' => 128],
In AdminProductsController.php (/controllers/admin/AdminProductsController.php), changed a part of function ajaxProcessAddAttachment(), from
if (!Validate::isGenericName($name)) {
$_FILES['attachment_file']['error'][] = $this->trans('Invalid name for %s language', [$language['name']], 'Admin.Notifications.Error');
} elseif (Tools::strlen($name) > 32) {
$_FILES['attachment_file']['error'][] = $this->trans('The name for %1s language is too long (%2d chars max).', [$language['name'], 32], 'Admin.Notifications.Error');
}
to
if (!Validate::isGenericName($name)) {
$_FILES['attachment_file']['error'][] = $this->trans('Invalid name for %s language', [$language['name']], 'Admin.Notifications.Error');
} elseif (Tools::strlen($name) > 64) {
$_FILES['attachment_file']['error'][] = $this->trans('The name for %1s language is too long (%2d chars max).', [$language['name'], 64], 'Admin.Notifications.Error');
}
But unfortunately, none of them worked.
Thanks in advance.
This is my own question but I found one solution.
In the AttachmentConstraint.php (in /src/Core/Domain/Attachment/Configuration/AttachmentConstraint.php), find the line
const MAX_NAME_LENGTH = 32;
and change it to whatever number that is lower than your database table can hold:
//const MAX_NAME_LENGTH = 32;
const MAX_NAME_LENGTH = 128; // this number needs to be lower than `name` column in `ps_attachment_lang` table
This should do the trick, but this solution requires changes in the core file. If there's anyone who knows a way we can achieve this without modifying the core files, please share that.

Embedding SVGs into PDFs with embedded fonts

I want to embed an SVG image into a PDF, and have the font families specified in the SVG text elements render using the correct fonts.
The fonts I am using are the 'Lato' TTF fonts.
Here is the code I have used to convert the fonts to TCPDF's "native format":
$variants = [
'Black',
'BlackItalic',
'Bold',
'BoldItalic',
'Hairline',
'HairlineItalic',
'Heavy',
'HeavyItalic',
'Italic',
'Light',
'LightItalic',
'Medium',
'MediumItalic',
'Regular',
'Semibold',
'SemiboldItalic',
'Thin',
'ThinItalic',
];
foreach ($variants as $variant) {
\TCPDF_FONTS::addTTFfont(
'/var/www/fonts/Lato-' . $variant . '.ttf',
'',
'',
32,
'/var/www/fonts/output/'
);
}
And here is the code I've used to generate a test PDF with an embedded SVG:
$svg_content = '<svg width="600px" height="800px">';
foreach ($variants as $key => $variant) {
$svg_content .= '<text x="30" y="' . ( 30 * ( $key + 1 ) ) . '" fill="#ED6E46" font-size="20" font-family="\'Lato-' . $variant . '\'" text-anchor="start">The quick brown fox jumped over the lazy dog (' . $variant . ').</text>';
}
$svg_content .= '</svg>';
$pdf = new TCPDF();
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetMargins(0, 0, -1, true);
$pdf->SetAutoPageBreak(false, 0);
$pdf->AddPage();
$pdf->AddFont('Lato-Black', '', '/var/www/fonts/output/latoblack.php');
$pdf->ImageSVG('#' . $svg_content, 0, 0, 300, 300, '', $align='', $palign='C', $border=0, false);
$pdf->Output(
preg_replace('/[^A-Za-z0-9]/', '', $influencer->getName()) . '.pdf',
'I'
);
The result I get is that all the text is rendered in what I assume is Helvetica:
If I add a line just before the call to ImageSVG() like this:
$pdf->SetFont('Lato-Black', '', null, '/var/www/fonts/output/latoblack.php');
Then all the text is rendered in Lato-Black:
It seems to me that the fonts are being embedded in the PDF fine, but the SetFont obviously sets the current font for subsequent PDF text elements, and the text elements within the SVG image render using this font.
My desired behaviour is that the font-family attributes defined on the SVG text elements are honoured within the PDF, so that each line renders using the font variant mentioned in the brackets at the end of the line.
How can I do this?
You could start with the following code and work backwards incorporating your changes.
To be clear I'm not specifically advocating this as the way to do it, only that this is one way that works and so should help as a starting point.
Font Converter:
$variants = [
'Black',
'BlackItalic',
'Bold',
'BoldItalic',
'Hairline',
'HairlineItalic',
'Heavy',
'HeavyItalic',
'Italic',
'Light',
'LightItalic',
'Medium',
'MediumItalic',
'Regular',
'Semibold',
'SemiboldItalic',
'Thin',
'ThinItalic',
];
foreach ($variants as $variant) {
\TCPDF_FONTS::addTTFfont(dirname(__FILE__) . '/fonts/Lato-' . $variant . '.ttf', 'TrueTypeUnicode', '', 96);
}
echo 'Done!';
This puts the .php, .z and .ctg.z files within /tecnickcom/tcpdf/fonts.
PDF Generator:
Note the different font-family names in the $variants array:
$variants = [
'lato',
'latob',
'latobi',
'latoblack',
'latoblacki',
'latohairline',
'latohairlinei',
'latoheavy',
'latoheavyi',
'latoi',
'latolight',
'latolighti',
'latomedium',
'latomediumi',
'latosemib',
'latosemibi',
'latothin',
'latothini'
];
$svg_content = '<svg width="600px" height="800px">';
foreach ($variants as $key => $variant) {
$svg_content .= '<text x="30" y="' . ( 30 * ( $key + 1 ) ) . '" fill="#ED6E46" font-size="20" font-family="\'' . $variant . '\'" text-anchor="start">The quick brown fox jumped over the lazy dog (' . $variant . ').</text>';
}
$svg_content .= '</svg>';
ob_start();
$pdf = new TCPDF();
$pdf->SetPrintHeader(false);
$pdf->SetPrintFooter(false);
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
$pdf->SetMargins(0, 0, -1, true);
$pdf->SetAutoPageBreak(false, 0);
$pdf->AddPage();
//$pdf->AddFont('latoblack', '', dirname(__FILE__) . '/fonts/output/latoblack.php');
$pdf->ImageSVG('#' . $svg_content, 0, 0, 300, 300, '', $align='', $palign='C', $border=0, false);
$pdf->Output('example_001.pdf', 'I');
Output:

Reflection Added to db but file does not exit

File is relative to /file/repository/AutoSalon/hasi/BB_Light_House_8.png
ipFileUrl(ipReflection('AutoSalon/'.$user['username'].'/'.$value['photo1'],$opti‌​ons,'thumb_'.$value['photo1'],true));
(36, options ,fprit, 'AutoSalon/hasi/BB_Light_House_8.png', '2016/02/03/thumb_BB_Light_House_8.png', 1454520313);
BUT there is no File in /file/2016/02/03/thumb_BB_Light_House_8.png
GET http://kukur.dev/file/2016/02/03/thumb_BB_Light_House_8_1.png 404 (Not Found)
Here is LOG FILE
{"errorTrace":"#0 /Users/flakerimi/Sites/ImpressPages/Ip/Internal/Repository/ReflectionModel.php(60): Ip\Internal\Repository\ReflectionModel->createReflectionRecord('AutoSalon/hasi/', Array, NULL)\n#1 /Users/flakerimi/Sites/ImpressPages/Ip/Internal/Repository/ReflectionService.php(84): Ip\Internal\Repository\ReflectionModel->getReflection('AutoSalon/hasi/', Array, NULL, true)\n#2 /Users/flakerimi/Sites/ImpressPages/Ip/Functions.php(957): Ip\Internal\Repository\ReflectionService->getReflection('AutoSalon/hasi/', Array, NULL, true)\n#3 /Users/flakerimi/Sites/ImpressPages/Plugin/AutoSalon/view/user/list.php(25): ipReflection('AutoSalon/hasi/', Array)\n#4 /Users/flakerimi/Sites/ImpressPages/Ip/View.php(111): require('/Users/flakerim...')\n#5 /Users/flakerimi/Sites/ImpressPages/Ip/Application.php(352): Ip\View->render()\n#6 /Users/flakerimi/Sites/ImpressPages/Ip/Application.php(442): Ip\Application->handleRequest(Object(Ip\Request), Array, false)\n#7 /Users/flakerimi/Sites/ImpressPages/Ip/script/run.php(8): Ip\Application->run()\n#8 /Users/flakerimi/Sites/ImpressPages/index.php(14): require_once('/Users/flakerim...')\n#9 {main}"}
Just for Archive purpose:
Problem was with $options,
$options = array('type' => 'center', 'widht' => 400, 'height'=>200, 'forced' => true);
There is a typo at Width (widht) and thats why. Maybe some better check on that ?

How can I change the checkboxlist to look similar to on/off switch?

Is there any way to change the checkboxlist widget to look similar to the on/off switch presented in Kartik's Switch Input Widget, but in a group?
I'm using this code:
echo $form->field($model, 'blocked_list')->checkboxList($array_list);
Which is very simple to use, but produces "simple" list...
I've tried with Karitk's like this:
foreach ($array_list as $category_id=>$category_name) {
echo '<label class="control-label">' . $category_name . '</label>';
echo SwitchInput::widget([
'name'=>'blocked_list',
'value'=>in_array($category_id, $model->blocked_list),
'pluginOptions' => [
'size' => 'mini',
],
]);
But it does not link with the model, and the Form looses it format
Any ideas, please?
I've got it!
foreach ($category_array as $category_id=>$category_name) {
if ( isset($model->blocked_list[$category_id]) )
$model->blocked_list[$category_id] = true;
else
$model->blocked_list[$category_id] = false;
echo $form->field($model, 'blocked_list[' . $category_id . ']')
->label($category_name)
->widget(SwitchInput::classname(), []);
}
I just added a validate "if" to ask if the variable to display exists, and then set the correct value for the model
Hope it helps someone

Attachment downloaded in "File" format instead of pdf

I am sending an email with an attachment (pdf) through laravel.But the attachment when downloaded converts to "File" type instead of pdf...
Heres my code
$pdf_file_path = public_path() . '/user/invoices/invoice_' . $data['hp_transaction_id'] . '.pdf';
PDF::loadView('payment.payment-invoice', $data)
->setPaper('a4')
->setOrientation('portrait')
->setWarnings(false)
->save($pdf_file_path);
Mail::send('emails.view-property-transaction-completed', $data, function($message) use ($pdf_file_path, $transaction) {
$message->to(Auth::user()->email)->subject('Transaction Completed');
$message->attach($pdf_file_path, array('as' => 'Invoice-' . $transaction->id, 'mime' => 'application/pdf'));
});
I want the file to get downloaded in pdf format.What should i do for that?
Found the solution. Add .pdf with the file name in as attribute, like this:
$message->attach(
$invoice_path,
array('as' => 'Invoice-' . $transaction->id. ".pdf", 'mime' => 'application/pdf')
);