Add text according to chosen image Moodle - php

In my Certificate plugin in Moodle, I can choose from 4 signatures. The signature images are shown in the certificate and loaded trough CERT_IMAGE_SIGNATURE:
certificate_print_image($pdf, $certificate, CERT_IMAGE_SIGNATURE, $sigx, $sigy, '', '');
The file names are jacob.png vanessa.png stan.png `lilly.png.
How to add automatically text under the image? For example the names of the person from the signature image?
If I choose the file jacob.png from the dropdown list, It should load under the image also the names, here "Jacob Svenson".

I would do something like this
in /mod/certificate/type/xxx/certificate.php
if (!empty($certificate->printsignature)) {
certificate_print_image($pdf, $certificate, CERT_IMAGE_SIGNATURE, $sigx, $sigy, '', '');
$signame = get_string($certificate->printsignature, 'certificate');
certificate_print_text($pdf, $x, $y + 20, 'C', $fontserif, '', 20, $signame);
}
and in /mod/certificate/lang/en/certificate.php use the file name as the string id.
$string['jacob.png'] = 'Jacob Svenson';

Related

create multiple pdf by id with TCPDF codeigniter

good evening ! I need your help, I am trying to generate several PDFs with the TCPDF, I have been able to create a PDF one by one by passing the ID of the database to the function, but what I need is for it to automatically create several PDFs for each ID that it brings from the database, how can I achieve this?
Controller:
public function reportpdf($id){
ob_start();
$allowance= $this->allowance_m->get_allowance_byid($id);
$this->load->library('tcpdf');
$pdf = new
TCPDF('P','mm','A4',true,'UTF-8',false);
$pdf->AddPage();
$pdf->setXY(12,40);
$txt_pembuka = 'Allowance Report';
//$pdf->SetFontSize(16);
$pdf->SetFont('times', 'B', 16, '', 'false');
$pdf->MultiCell(0, 5, $txt_pembuka, 0, 'C', 0, 2, '', '', true);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->SetFont('times', '', 12, '', 'false');
$pdf->cell(35,5,"Nama");
$pdf->cell(0,5,": ".$allowance->tarjeta);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Periode");
$periode = strtotime($allowance->monto);
$formatperiode = date('F Y',$periode);
$pdf->cell(0,5,": ".$formatperiode);
$pdf->setXY(20,$pdf->getY()+7);
$pdf->cell(35,5,"Uang Makan");
$pdf->cell(0,5,": Rp.".$allowance->nombre);
$pdf->Output();
ob_end_flush();
}
Model:
public function get_allowance_byid($id)
{
$db6 = $this->load->database('db6', TRUE);
$sql = "SELECT * FROM registros WHERE tarjeta='".$id."'";
return $db6->query($sql)->row();
}
If you want to create "several PDFs for each ID" it is recommended to first save the created PDF files in a local folder on your server before making them available to your user instead of prompting the created PDF directly in the browser as in your example. It is not practical to send multiple PDFs at once to the browser.
Add a path and a filename to the Output Method to save the PDF, preferably with an ID so you can find it again:
$pdf->Output('allowance_report_'.$id.'.pdf');
Documentation: https://tcpdf.org/docs/srcdoc/TCPDF/classes-TCPDF/#method_Output
In your case, you can either create multiple PDFs inside the method reportpdf() or make a new method for each file.
If you choose to do it inside your current method you can do something like:
public function reportpdf($id){
$allowance= $this->allowance_m->get_allowance_byid($id);
$this->load->library('tcpdf');
//CREATE YOUR FIRST PDF....
$pdf = new
TCPDF('P','mm','A4',true,'UTF-8',false);
$pdf->AddPage();
... ETC ...
// SAVE THE FIRST PDF BY PROVIDING A PATH AND A FILENAME
$pdf->Output('folder/allowance_report_'.$id.'.pdf');
//CREATE YOUR SECOND PDF....
$pdf = new
TCPDF('P','mm','A4',true,'UTF-8',false);
$pdf->AddPage();
... ETC ...
// SAVE THE SECOND PDF BY PROVIDING A PATH AND A FILENAME
$pdf->Output('folder/another_report_'.$id.'.pdf');
}
If you then want to send the PDFs to the browser I suggest you create a zip file containing all the created PDFs and prompt it through the browser.

How can reduce height and download pdf using FPDF?

I have a function to generate pdf using Fpdf in laravel.
My problems are:
After all Cell I have some extra space. I need to remove that. Please find the image given below.
How can I download this pdf file in to my system. Currently it's just showing in to browser. Code samples are given below.
Code
Controller: Controller.php
public function index()
{
$orders = Order::select('firstname', 'lastname', 'street', 'postal', 'country')->get();
foreach ($orders as $order){
Fpdf::SetMargins(5, 5, 5);
Fpdf::AddPage('L', array(60,90), 'A4');
Fpdf::SetAutoPageBreak(TRUE, 0);
Fpdf::SetFont('helvetica', '', 7); //IF bold letter SetFont('Arial','B',14)
Fpdf::SetTextColor(0, 0, 0);
Fpdf::Cell(10,5,iconv('UTF-8', 'windows-1252', 'Falls unzustellbar, zurück an Absender'),0,"1","L");
Fpdf::SetFont('','U');
Fpdf::Cell(10,5,iconv('UTF-8', 'windows-1252', 'schrillALARM.jetzt c/o 365group • Grasgasse 2 • 93047 Regensburg'),0,"1","L");
Fpdf::SetFont('helvetica', '', 11);
Fpdf::Cell(10,5,$order->firstname,0,1,"L");
Fpdf::Cell(10,5,$order->lastname,0,1,"L");
Fpdf::Cell(10,5,$order->street,0,1,"L");
Fpdf::Cell(10,5,$order->postal,0,1,"L");
Fpdf::Cell(10,5,$order->country,0,1,"L");
}
Fpdf::Output();
exit;
}
Route: Route::get('/test', 'Controller#index');
No experience with FDPF, but you can download this way:
Route::get(
'download/pdf/{pdf}',
function ($pdf) {
$file = // Get file
return response()->download($file);
}
);
Or just from your controller with
return response()->download($pdf);
for saving, just specify output path and filename in your output call string
Fpdf::Output([string dest [, string name [, boolean isUTF8]]])
For your white space though, when you're constructing your PDF document, you can use a default size of one of the following: A3, A4, A5, Letter, Legal with A4 being default. However, you can also declare custom sizes. This is more than likely what you're looking for, as you'll want to play with the sizes to get it with the amount of white space you're looking for. FPDF puts out the canvas first then fills it, so you're white space is coming from a canvas that is too big. This can be done in the constructor, or AddPage as you have done.
VIA Constructor:
//(L)andscape or (P)ortrait, unit type (mm millimeters), array size in mm
$pdf = new FPDF('L','mm',array(100,150));
VIA AddPage (must likely what you're looking for):
currently you have:
Fpdf::AddPage('L', array(60,90), 'A4');
however, params are supposed to be landscape/portrait, Predefined or custom size array, then rotation. So try this:
Fpdf::AddPage('L', array(60,90));
Now you'll need to play with those numbers, more than likely the 90, and shorten that up to rid yourself of the white space.

Internal linking in tcpdf

I am using TCPDF to create simple pdf document.
I am creating a page and adding link using below code
$pdf->addTOCPage();
$link = $pdf->AddLink();
$pdf->SetLink($link, 0, -1);
Now link is set successfull.But to navigate to that page what should I add ?
I tried below code , but it does nothing,
Return to TOC
// Create a fixed link to the first page using the * character
$index_link = $pdf->AddLink();
$pdf->SetLink($index_link, 0, '*1');
$pdf->Cell(0, 10, 'Link to INDEX', 0, 1, 'R', false, $index_link);
http://www.tcpdf.org/examples/example_045.phps
update -
refer to this function addHtmlLink() in tcpdf library.
You can add a internal link through this
$pdf->addHtmlLink('#'.$index_link, 'hello');
where 'hello' begin the name of anchor and and first param being identifier to the link.
In your case
$pdf->addHtmlLink('#'.$link, 'Whatever you like to name it');
$html = 'link name';
$pdf->writeHTML($html, true, false, true, false, '');

How to rotate full page with tcpdf?

I am using TCPDF library to write a custom size label with background image and multiple text blocks on it.
The user when sees the preview on screen of the PDF it should show in horizontal, but for printing, I need the full page rotated -90 degrees.
How can I just rotate the whole page for printing version without having to move anything?
Basically:
In my case I've already had to use a new document format for the special sizes my document required.
So I've duplicated that format, created one for Landscape and one for Portrait.
Then based on the $preview variable, if previewing I'm rendering the normal landscape document, but if not previewing, I'm using the Portrait format and orientation and also starting the transformation and rotating everything on page.
Hope this helps someone I've found no other "quick" way to accomplish this kind of full-page rotation.
<?php
// #1 get the preview attribute from
// the form that was submitted from the user
$preview= isset($_POST['preview'])?(int)$_POST['preview']:0;
// load TCPDF for CodeIgniter as a library
$this->load->library('Pdf');
// #2 set default orientation and format
$orientation='L';
$format='MAKE-L';
// #3 if not previewing, switch orientation and format to portrait
if (!$preview) {
$orientation='P';
$format='MAKE-P';
}
// create new pdf object
// (same as doing new TCPDF(), it is just the CodeIgniter wrapper)
$pdf = new Pdf($orientation, 'mm', $format, true, 'UTF-8', false);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(0, 0, 0);
$pdf->AddPage($orientation, $format);
// #4 if not previewing, start transformation
// and rotate everything before inserting any content
if (!$preview) {
// Start Transformation
$pdf->StartTransform();
// Rotate counter-clockwise centered by (x,y)
$pdf->Rotate(-90, 70, 70); // <-- TODO: test this very well because 70 and 70 was just guessing, there is no math behind that so not sure if it will work always
}
// put your content here,
// for example set font and add a text
$pdf->SetFont('times', '', 7, '', true);
$pdf->writeHTMLCell(0, 0, 25.4, 2, 'lot number', 0, 1, 0, true, '', true);
/// end content
// #5 if not in preview mode, finish the transformation
if (!$preview) {
// Stop Transformation
$pdf->StopTransform();
}
$pdf->Output('example.pdf', 'I');
/**
* Last but very important note:
* I have added my formats in tcpdf/includes/tcpdf_static.php file.
* >> MAKE-L for Landscape
* >> MAKE-P for Portrait
*/
public static $page_formats = array(
// Make
'MAKE-L' => array( 396.850, 425.196), // = ( h 140 x w 150 ) mm
// Make
'MAKE-P' => array( 425.196, 396.850 ), // = ( h 140 x w 150 ) mm
// .. rest of formats here ../
);
The setPageFormat() method should do the job. You also can pass the parameter to the $format parameter of AddPage():
$pdf->AddPage($orientation, ['format' => $format, 'Rotate' => -90]);

TCPDF table of contents and issues with page numbers

I'm using the TCPDF library to generate PDFs with PHP/HTML. The docs can be found here: http://www.tcpdf.org/doc/code/classTCPDF.html.
I am trying to create a table of contents (as demonstrated in example 59 on the TCPDF site) but I'm experiencing a few issues:
1) The Table of Contents page number is showing as the last page of the document. (8 of 8 when in reality it's 2 of 8, comes after a cover page).
2) The page numbers on the other pages are not adjusting. The page after the TOC should be 3 of 8, but instead says 2 of 8.
3) The table of contents has the correct page numbers for the bookmarks, but these numbers don't match the page numbers on those pages (related to issue #2).
How I'm generating bookmarks:
I'm generating bookmarks for each page by calling the Bookmark() method after each added page.
$pdf->AddPage();
$pdf->Bookmark('Chapter 1', 0, 0, '', 'B', array(0,64,128));
How I'm generating the table of contents:
This is ripped directly from Example 59 linked above. In that example, the scenario is slightly different as it does not have a cover page.
// add a new page for TOC
$pdf->addTOCPage();
// write the TOC title and/or other elements on the TOC page
$pdf->SetFont('times', 'B', 16);
$pdf->MultiCell(0, 0, 'Table Of Content', 0, 'C', 0, 1, '', '', true, 0);
$pdf->Ln();
$pdf->SetFont('helvetica', '', 10);
// define styles for various bookmark levels
$bookmark_templates = array();
/*
* The key of the $bookmark_templates array represent the bookmark level (from 0 to n).
* The following templates will be replaced with proper content:
* #TOC_DESCRIPTION# this will be replaced with the bookmark description;
* #TOC_PAGE_NUMBER# this will be replaced with page number.
*
* NOTES:
* If you want to align the page number on the right you have to use a monospaced font like courier, otherwise you can left align using any font type.
* The following is just an example, you can get various styles by combining various HTML elements.
*/
// A monospaced font for the page number is mandatory to get the right alignment
$bookmark_templates[0] = '<table border="0" cellpadding="0" cellspacing="0" style="background-color:#EEFAFF"><tr><td width="155mm"><span style="font-family:times;font-weight:bold;font-size:12pt;color:black;">#TOC_DESCRIPTION#</span></td> <td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:12pt;color:black;" align="right"> #TOC_PAGE_NUMBER#</span></td></tr></table>';
$bookmark_templates[1] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="5mm"> </td><td width=" 150mm"><span style="font-family:times;font-size:11pt;color:green;">#TOC_DESCRIPTION#</span></td><td width="25mm"><span style="font-family:courier;font-weight:bold;font-size:11pt;color:green;" align="right">#TOC_PAGE_NUMBER#</span></td></tr ></table>';
$bookmark_templates[2] = '<table border="0" cellpadding="0" cellspacing="0"><tr><td width="10mm"> </td><td width=" 145mm"><span style="font-family:times;font-size:10pt;color:#666666;"><i>#TOC_DESCRIPTION#</i></span></td><td width="25mm "><span style="font-family:courier;font-weight:bold;font-size:10pt;color:#666666;" align="right">#TOC_PAGE_NUMBER#</span ></td></tr></table>';
// add other bookmark level templates here ...
// add table of content at page 1
// (check the example n. 45 for a text-only TOC
$pdf->addHTMLTOC(2, 'INDEX', $bookmark_templates, true, 'B', array(128,0,0));
// end of TOC page
$pdf->endTOCPage();
How I'm getting the page numbers for the footers:
// Page footer
public function Footer() {
$pageN = $this->PageNo();
if($pageN === 1){
// Do nothing
} else {
// Position at 15 mm from bottom
$this->SetY(-15);
// Set font
$this->SetFont($helvetica_light, '', 10);
$this->setTextColor(255,255,255);
// Page number
$this->Cell(0, 12, ' '.$this->PageNo().' of '.$this->getNumPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
}
}
Basically, I'd like the Table of Contents to fall on page 2, with a footer that says Page 2, all subsequent page numbers to be labeled correctly. How can I do this? I can clarify the question/code if needed.
Any help is appreciated :)
PageNo() is a bit funky when it comes to calculating pages. It outputs what page it is in order of creation, and since the ToC page is created as the very last it will show that as the last page.
So instead use
// Page number
$this->Cell(0, 12, ' '.$this->getAliasNumPage().' of '.$this->getAliasNbPages(), 0, false, 'C', 0, '', 0, false, 'T', 'M');
The rest of the code you provided, like the check if it is the first page, should work with PageNo().

Categories