I'm trying to load a existing pdf file, and fill this with database information. Loading the file and everything is working, except for writing data to the loaded page. It doesn't write text to the loaded page. If I add a new page en use a foreach to apply drawing to all pages, all added pages are written, except for the loaded one. Below is the code I'm using:
$pdf = Zend_Pdf::load('./documents/agreements/_root/gegevens.pdf'); // Load pdf
$pdf->pages = array_reverse($pdf->pages); // reverse pages
$pdf->pages[] = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4); // Add a page (A4)
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); // Set font
foreach($pdf->pages as $page) // Apply settings+text to every page (total of 2)
{
$page->setFont($font, 36);
$page->setAlpha(0.25);
$page->drawText('LALALALALALALA', 62, 260, 'UTF-8');
}
$pdf->save('./documents/agreements/Gegevens_'.$this->school_id.'.pdf'); // Save file
I solved the problem: I created a new pdf file with different settings. Creating the pdf with the following settings (I use Acrobat PDFmaker Office COM Addin for word) did the trick. I guess the code was working after all, the pdf itself was causing the problems.
In word select save as PDF
Select the 'Quick and simple PDF' format
Change the settings in 'Adobe PDF conversion options':
Enable -> Convert document information
Enable -> Make PDF/A Compliant
Disable -> Create bookmarks from
Disable -> Convert Comments
Note: This regards saving a file as PDF in word. Other office applications aren't tested.
Try the following:
$pdf = Zend_Pdf::load('./documents/agreements/_root/gegevens.pdf'); // Load pdf
$font = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA); // Set font
foreach($pdf->pages as $pid => $page) // Apply settings+text to every page (total of 2)
{
$myPage = new Zend_Pdf_Page($page);
$myPage->setFont($font, 36);
$myPage->setAlpha(0.25);
$myPage->drawText('LALALALALALALA', 62, 260, 'UTF-8');
$pdf->pages[$pid] = $page;
}
$pdf->save('./documents/agreements/Gegevens_'.$this->school_id.'.pdf'); // Save file
try this one.
$pdf->pages[i]->drawText('LALALALALALALA', 62, 260, 'UTF-8');
Related
Im generating a PDF using FPDF which works fine, when certain conditions are met i want to be able to import an existing PDF (sometimes with multiple pages), what im finding though is the FPDI import is overwriting any existing FPDF page generation.
Here is some sample code
if($row['art'] <> '')
{
$datasubject = mysqli_query($link,"SELECT * from documents WHERE subject = 'Art'");
$rowsubject = mysqli_fetch_array($datasubject);
$pdf->AddPage("P","A4");
$pdf->SetY(30);
$pdf->cell(350,20,"Art Page",0,'C',false);
$pdf = new \setasign\Fpdi\Fpdi();
// set the source file
$pageCount = $pdf->setSourceFile("C:\\temp\\sourcedocuments\\Year 7\\art\\KS4 FINE ART KNOWLEDGE ORGANISER higher tier.pdf");
for ($pageNo = 1; $pageNo <= $pageCount; $pageNo++) {
$tplIdx = $pdf->importPage($pageNo);
// add a page
$pdf->AddPage();
$pdf->useTemplate($tplIdx, null, null, 210, 300, true);
// font and color selection
$pdf->SetFont('Helvetica');
$pdf->SetTextColor(200, 0, 0);
}
}
Before it hits this "IF " statement FPDF creates a couple of static "Cover" pages but these are also overwritten by the imported document.
Moved $pdf = new \setasign\Fpdi\Fpdi(); from out of the "If" statement and replaced
$pdf = new FPDF(); with $pdf = new \setasign\Fpdi\Fpdi();
Save and display tinymce content in Moodle.
I have a block that save question and answer in db.
I use tinymce editor for this, so that user can enter text and image.
My editor form is:
.....
$editoroptions = array('maxfiles' => EDITOR_UNLIMITED_FILES, 'noclean'=>true, 'context'=>$context);
$mform->addElement('editor', 'title_editor', 'Questions', null, $editoroptions);
$mform->addRule('title_editor', null, 'required', null, 'client');
$mform->setType('title_editor', PARAM_RAW);
.....
I submit the form and save the data(text+image) from tinymce in db
......
if($data = $sample_form->get_data()) {
if ($draftitemid = file_get_submitted_draft_itemid('title_editor')) {
$data->title_editor['text'] = file_save_draft_area_files($draftitemid, $contextid, 'block_sample', questiontext, array('subdirs' => true, 'maxfiles' => 5),$data->title_editor['text']);
}
//insert to database
$inserRecord = new stdClass();
$inserRecord->suggestion = $sgid;
$inserRecord->questiontext = $data->title_editor['text'];
$inserRecord->answertext = $data->answer['text'];
$insertRes = add_question_desc($inserRecord);
......
In db the data(here question and answer) saved. The question data is looks like:
<p>What color is this?</p>
<p><img src="##PLUGINFILE##/sample_image.png" width="309" height="212" alt="green" /></p>
Is this complete to save the data? Where did the uploaded file saved. How I retreive/display the uploaded file.
I use:
$qn = file_rewrite_pluginfile_urls($qnDetails[$qnid]->questiontext, "pluginfile.php", $context->id, "block_sample", 'questiontext', $qnid);
echo $qn;
The above code only display the text and image is not displaying.
I inspect the broken image field and it is:
<img src="http://localhost/moodle/pluginfile.php/24/block_sample/questiontext/12/mc4.png" width="309" height="212" alt="mc4.png">
To manipulate files in an editor you must use the following methods:
Prior to displaying the form: file_prepare_standard_editor()
When saving the form: file_postupdate_standard_editor()
When displaying the content: file_rewrite_pluginfile_urls() followed by format_text()
You can find an example of this in cohort/edit.php and cohort/index.php.
Once that is done, you need to implement the function _pluginfile which Moodle core will call to get the file. The _pluginfile functions are required so that your plugin can check whether or not the user can access the file. You can find default implementations in filelib.php file_pluginfile() and in various lib.php <component_name>_pluginfile().
Within a pdf it is possible to change page numbering, so the first page would be page 5, etc.
(This has nothing to do with headers and footers, i'm speaking strictly about the page numbers as they appear in the pdf toolbar)
Is it possible to control those numbers with EvoPDF?
Yes, apparently with EVOPDF v5 you can set the number to be displayed on the page using the PageNumberingStartIndex property of the PdfHeaderOptions object (same for Footers). I don't know of any examples using this.
It is not possible to change the page numbering displayed by Adobe Reader using an option in the generated PDF document. What you can do is to make the PDF viewer go to a certain page in PDF document when the document is opened. You can check the Go To a Location in a PDF Page When the Document is Opened Demo . The C# code to implement this feature is:
protected void convertToPdfButton_Click(object sender, EventArgs e)
{
// Create a HTML to PDF converter object with default settings
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
// Set license key received after purchase to use the converter in licensed mode
// Leave it not set to use the converter in demo mode
htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
Document pdfDocument = null;
try
{
// Convert a HTML page to a PDF document object
pdfDocument = htmlToPdfConverter.ConvertUrlToPdfDocumentObject(urlTextBox.Text);
int goToPageNumber = int.Parse(pageNumberTextBox.Text);
if (goToPageNumber > pdfDocument.Pages.Count)
{
return;
}
// Get destination PDF page
PdfPage goToPage = pdfDocument.Pages[goToPageNumber - 1];
// Get the destination point in PDF page
float goToX = float.Parse(xLocationTextBox.Text);
float goToY = float.Parse(yLocationTextBox.Text);
PointF goToLocation = new PointF(goToX, goToY);
// Get the destination view mode
DestinationViewMode viewMode = SelectedViewMode();
// Create the destination in PDF document
ExplicitDestination goToDestination = new ExplicitDestination(goToPage, goToLocation, viewMode);
// Set the zoom level when the destination is displayed
if (viewMode == DestinationViewMode.XYZ)
goToDestination.ZoomPercentage = int.Parse(zoomLevelTextBox.Text);
// Set the document Go To open action
pdfDocument.OpenAction.Action = new PdfActionGoTo(goToDestination);
// Save the PDF document in a memory buffer
byte[] outPdfBuffer = pdfDocument.Save();
// Send the PDF as response to browser
// Set response content type
Response.AddHeader("Content-Type", "application/pdf");
// Instruct the browser to open the PDF file as an attachment or inline
Response.AddHeader("Content-Disposition", String.Format("attachment; filename=Go_To_Page_Open_Action.pdf; size={0}", outPdfBuffer.Length.ToString()));
// Write the PDF document buffer to HTTP response
Response.BinaryWrite(outPdfBuffer);
// End the HTTP response and stop the current page processing
Response.End();
}
finally
{
// Close the PDF document
if (pdfDocument != null)
pdfDocument.Close();
}
}
I have an existing pdf, and i want to load it, change its orientation and save it. I tried this but it doesn't work :
$pdf = Zend_Pdf::load('mypdf');
$page = $pdf->pages[0];
$page->rotate(0,0,deg2rad(90));
$pdf->save('new.pdf');
Any idea?
You have to assign the page $page to the PDF document $pdf after you rotated the page:
$page->rotate(0,0,deg2rad(90));
$pdf->pages[0] = $page; // assign the page to the PDF document
$pdf->save('new.pdf');
Be aware that in the above example the old page 1 is overwritten in the $pdf object.
I have problems creating a template for my pdf documents in Zend_Pdf. I've followed the guide provided at http://framework.zend.com/manual/en/zend.pdf.pages.html but it doesn't seem to work since no footer text or logo are visible at any page except the first.
This is a sample of my code: (Note; the MTP constant is just the recalculate from mm to points)
//Create pdf object
$pdf = new Zend_Pdf();
//Create the first page
$page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
/*HEADER*/
//insert logo at the top
$logo = Zend_Pdf_Image::imageWithPath(APPLICATION_PATH . '/../document_root/images/logotype.png');
$page->drawImage($logo, 22*MTP,274*MTP, 62*MTP, 289*MTP);
/*FOOTER*/
$footerStyle->setFont(Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA), 10);
$page->setStyle($footerStyle);
$page->drawText('Footer text', 33*MTP, 20*MTP, 'ISO-8859-1');
//save the page to pdf->pages
$pdf->pages[0] = $page;
//save the page as a template
$template = $pdf->pages[0];
//create the next page from template
$page1 = new Zend_Pdf_Page($template);
//save the next page to pdf->pages
$pdf->pages[] = $page1;
Have I completely misunderstood how this "template function" work in Zend_Pdf? I know that Zend_Pdf lacks quite a number of features compaired to some of the external pdf-generators (like FPDF) but still, there must be some way to make a basic header/footer for all pages right?
Try to use clone instead passing page as argument to a new page.
$page1 = clone $template;
or simply
$pdf->pages[] = clone $pdf->pages[0];