Add div to existing pdf laravel [duplicate] - html

Laravel Package:
"setasign/fpdi": "^2.3",
"setasign/fpdf": "^1.8"
$pdf = new \setasign\Fpdi\Fpdi('L','mm','A4');
$pageCount = $pdf->setSourceFile(public_path().'/'.$url);
$pdf->setFont('Arial', 'B', 10);
for($i = 1; $i <= $pageCount; $i++){
$tplIdx = $pdf->importPage($i);
$pageDimensions = $pdf->getImportedPageSize($tplIdx);
$pdf->addPage($pageDimensions['orientation'], $pageDimensions);
$pdf->useTemplate($tplIdx);
}
If It Possible read the content of the last page and get after page content ordinate of the current position. then Write new content without add new page or whitespaces

If Pdf Generate using DomPdf so you can generate HTML file using
$pdf->getDomPDF()->outputHtml()
Generated html file using DomPDF add some element like this:
<div id="divId"></div>
Then whenever you need to edit this document, you need to read the HTML file line by line and
Edit document:
$url=public_path() .'/test.html';
$htmlFile='';
foreach(file($url) as $key => $line){
if(strpos($line, 'id="divId"') !== false){
$line='<div>Hello world</div>';
}
$htmlFile.=$line;
}
$pdf = PDF::loadHTML($htmlFile)->setPaper('a4', 'landscape');
$pdf->output();

Related

TYPO3 templates and changes the div id

I have a Template in TYPO3 that i want to use to some pages, in here i have a DIV.
Is it possible depending on the page UID, to changes the DIV ID. its the only div thats changes content/image, and im looking to put this DIV inside my main.html template.
so if
UID = 2 <div id="topbanner_about"></div>
UID = 3 <div id="topbanner_drills"></div>
and so on....
Can i do this, and can i do it in TS (Typo Script) or how can i do this, so i dont need to make 5 templates.
You can do this by inserting a marker in your template. It would look somehow like this:
In the template:
[...]
<div id="topbanner_###ID_SUFFIX###"></div>
[...]
In the TypoScript, where the template is inserted:
10 = TEMPLATE
10 {
template = FILE
template.file = fileadmin/main.html
marks {
ID_SUFFIX = TEXT
ID_SUFFIX {
insertData = 1
# This makes sure that the output is valid and prevents XSS attacks
htmlSpecialChars = 1
value = {page:uid} # Use this to insert the page ID or
value = {page:subtitle} # Use to insert subtitle of page
... # Same works for other fields of the page record.
}
}
}
If the fields provided by default pages are not enough, you could add another field to the page records. The best way to do that would be to build an extension that does it.
I found a better solution, if i use ressource and add an image and on the next page do the same just with another images, then in my main TS i add this code.
lib.imageElement = FILES
lib.imageElement {
references {
data = levelmedia:-1,slide
listNum = 0
}
renderObj = COA
renderObj {
10 = IMAGE
10 {
file.import.data = file:current:originalUid
altText.data = file:current:title
}
}
}
It do the trick, then it shows a diffrent image in the top/header on every page.
But thx...

Saving text box input to XML or txt file in HTML

I'm working on a HTML page project where I have 2 text boxes and basically I want to save the input data that the user put in the text boxes. What we did in my C# class was that we saved all input into a XML file so I'm assuming there is a similar way? Either to a XML or some other file that can store text?
Anyone that knows a solution?
I recommend the following php script
<?php
// check that form was submitted
// (you'll need to change these indices to match your form field names)
if( !empty( $_POST['firstname'] ) && !empty( $_POST['lastname'] ) ){
// remove html tags from submission
// (since you don't want them)
$firstname = strip_tags( $_POST['firstname'] );
$lastname = strip_tags( $_POST['lastname'] );
// create the date
// (you can change the format as desired)
$date = date( 'Y-m-d' );
// create an array that holds your info
$record = array( $firstname,$lastname,$date );
// save the record to your .txt file (I still recommend JSON)
$json = json_encode( $record );
$file = '/_server_/path/to/yourfile.txt';
file_put_contents( $json,$file );
}

PHP DomNode->insertBefore()

I am trying to insert nodes in my html string.
My goal is to insert an element before each h2 tag.
For that, I am using:
$htmlString = "<h2>some html</h2>";
$DOM = new DOMDocument();
$DOM->loadHTML($htmlString);
$itemTitles = $DOM->getElementsByTagName('h2');
for($i = 0; $i < $itemTitles->length; $i ++)
{
$helpNavigatorContents[] = $itemTitles->item($i)->nodeValue;
$textBefore = new DOMNode(
'<a name="'.$itemTitles->item($i)->nodeValue.'"></a>'
);
$itemTitles->item($i)->parentNode->insertBefore(
$textBefore,
$itemTitles->item($i)
);
}
$htmlString = $DOM->saveHTML($DOM);
And here I have a problem with the $textBefore. When I declare the $textBefore as a DOMText, I can insert the text before the node but when I try this with DOMNode, then I am getting the following error (Demo):
Warning: DOMNode::insertBefore(): Couldn't fetch DOMNode
The code doesn't make any sense. DOMNode does not have a constructor. It is not supposed to be created at all. You are supposed to create specific node types through DOMDocument to have them associated with the Document.
Assuming you want to prepend all the H2 element with an anchor, this is how to do it:
libxml_use_internal_errors(true);
$DOM = new DOMDocument();
$DOM->loadHTML($htmlString);
$DOM->preserveWhiteSpace = false;
foreach ($DOM->getElementsByTagName('h2') as $h2) {
$a = $DOM->createElement('a');
$a->setAttribute('name', $h2->nodeValue);
$h2->parentNode->insertBefore($a, $h2);
}
$DOM->formatOutput = true;
echo $DOM->saveHTML();
Demo http://codepad.org/N0dPcLwT
To wrap the H2 elements into the A element, simply do the same and add
$a->appendChild($h2);
Demo http://codepad.org/w7Hi0Bmz

Basic information extraction from html?

I have a project where users submit many links to external sites and I need to parse the HTML of these submitted links and extract basic information from the page in the same way that Digg and Facebook do when a link is submitted.
I want to retrieve:
main title or heading (could be in title, h1, h2, p etc...)
intro or description text (could be in div, p etc...)
main image
My main problem is that there seem to be too many options to explore here and im getting a little confused to sat the least. Many solutions I have looked so far seem to be inadequate or huge overkill.
You would pick a server side language to do this.
For example, with PHP, you could use get_meta_tags() for the meta tags...
$meta = get_meta_tags('http://google.com');
And you could use DOMDocument to get the title element (some may argue if needing the title element, you may as well use DOMDocument to get the meta tags as well).
$dom = new DOMDocument;
$dom->loadHTML('http://google.com');
$title = $dom
->getElementsByTagName('head')
->item(0)
->getElementsByTagName('title')
->item(0)
->nodeValue;
As for getting main image, that would require some sort of extraction of what may be considered the main image. You could get all img elements and look for the largest one on the page.
$dom = new DOMDocument;
$dom->loadHTML('http://google.com');
$imgs = $dom
->getElementsByTagName('body')
->item(0)
->getElementsByTagName('img');
$imageSizes = array();
foreach($imgs as $img) {
if ( ! $img->hasAttribute('src')) {
continue;
}
$src = $img->getAttribute('src');
// May need to prepend relative path
// Assuming Apache, http and port 80
$relativePath = rtrim($_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'], '/') . '/';
if (substr($src, 0, strlen($relativePath) !== $relativePath) {
$src = $relativePath . $src;
}
$imageInfo = getimageinfo($src);
if ( ! $imageInfo) {
continue;
}
list($width, $height) = $imageInfo;
$imageSizes[$width * $height] = $img;
}
$mainImage = end($imageSizes);

PHP: Inject iframe right after body tag

I would like to place an iframe right below the start of the body tag. This has some issues since the body tag can have various attributes and odd whitespace. My guess is this will will require regular expressions to do correctly.
EDIT: This solution has to work with php 4 & performance is a concern of mine. It's for this http://drupal.org/node/586210#comment-2567398
You can use DOMDocument and friends. Assuming you have a variable html containing the existing HTML document as a string, the basic code is:
$doc = new DOMDocument();
$doc->loadHTML(html);
$body = $doc->getElementsByTagName('body')->item(0);
$iframe = $doc->createElement('iframe');
$body->insertBefore($iframe, $body->firstChild);
To retrieve the modified HTML text, use
$html = $doc->saveHTML();
EDIT: For PHP4, you can try DOM XML.
Both PHP 4 and PHP 5 should be happy with preg_split():
/* split the string contained in $html in three parts:
* everything before the <body> tag
* the body tag with any attributes in it
* everything following the body tag
*/
$matches = preg_split('/(<body.*?>)/i', $html, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
/* assemble the HTML output back with the iframe code in it */
$injectedHTML = $matches[0] . $matches[1] . $iframeCode . $matches[2];
Using regular expressions brings up performance concerns... This is what I'm going for
<?php
$html = file_get_contents('http://www.yahoo.com/');
$start = stripos($html, '<body');
$end = stripos($html, '>', $start);
$body = substr_replace($html, '<IFRAME INSERT>', $end+1, 0);
echo htmlentities($body);
?>
Thoughts?