PHPexcel load an html formatted variable - phpexcel

i am trying to load an html snippet variable into a spreadsheet.I have differnet sections which may have different dynamic html content. So i cant just convert manually.
$dynamic_code_snippet = '<ul><li>item one</li><li>item two</li></ul>';
At the moment i am trying this but get errors:
if(!empty($dynamic_code_snippet)){
$excelHTMLReader = PHPExcel_IOFactory::createReader('HTML');
$snippet = $excelHTMLReader->load($dynamic_code_snippet);
$snippetWriter = PHPExcel_IOFactory::createWriter($snippet,'Excel2007');
ob_start();
$snippetWriter->save('php://output');
$code = ob_get_clean();
$table->getActiveSheet()->setCellValue('A1, $code);
}
I get an error:
Fatal error: Uncaught PHPExcel_Reader_Exception: Could not open in PHPExcel\Reader\Abstract.php on line 196
Do you think its because its a snippet not a full page html document(no <html><head><body> tags)?
Can it be done? or do i have to rethink?

PHPExcel does provide a helper wizard that can be used to convert basic html markup to Riche Text Objects that can be set as a cell value:
$wizard = new PHPExcel_Helper_HTML;
$richText = $wizard->toRichTextObject($html);
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1', $richText);
However, only some markup tags are actively supported, and ordered/unordered lists aren't included.
If you want to take a look at it, and see how the code actually works, possibly modify it to work with lists, then it's the HTML.php file in /Classes/PHPExcel/Helper.

Related

This block contains invalid or unexpected content on Custom HTML

I am editing a document in draft mode Wordpress 5.2.2 in the Gutenberg editor, and add this Custom HTML block:
<pre><code class="language-typescript">const simple = &ltT&gt(cl: T) => cl;
class Hero {
constructor(public position: [number, number]) {}
}
interface { hello: number }
const errorOne = &ltT&gt(cl: T) => new cl(); // Cannot use 'new' with an expression whose type lacks a call or construct signature.</code></pre>
and it happily works as expected in preview. I save as draft.
When I return the HTML is ghosted and I get the error in the title. I can convert to HTML and it works again, but then it errors again when I return to it later.
It seems this error is talked about everywhere but the explanations are nonsense and resolve nothing.
If my Custom HTML is valid (which it seems to be), why does it work and then give an error. How do I fix this?
I think the main issue is not converting < & > properly in your code. They are missing the semicolon at the end of the string.
This code is working fine:
<pre><code class="language-typescript">const simple = <T>(cl: T) => cl;
class Hero {
constructor(public position: [number, number]) {}
}
interface { hello: number }
const errorOne = <T>(cl: T) => new cl(); // Cannot use 'new' with an expression whose type lacks a call or construct signature.</code></pre>
When you insert the code with missing semicolon, WordPress saved as is. However, when trying the load the page again WordPress compares the saved content (with missing characters) to the one generated from the block (which is probably attempting to display correct HTML). This process led to an error as both texts were not identical.
If you want to check the error yourself you can check the console through developer tool in your browser (F12 in Chrome).

How to display Link url as plain text which is added from ck-editor link element?

I want display the link url as plain text witch added by link element from ck-editor .
below is my code
var medsims_val = '{external_link{imageurl:"[medsims_img_url]", linkurl:"[medsims_link]", expiry_date:"[medsims_link_expiry]",caption:"[medsims_caption_text]"}}';
var medsims_field_val = this.getValueOf('info', 'medsims_img_url');
medsims_val = medsims_val.replace("[medsims_img_url]", (medsims_field_val ? medsims_field_val.trim() : ''));
medsims_field_val = this.getValueOf('info', 'medsims_link');
medsims_val = medsims_val.replace("[medsims_link]", (medsims_field_val ? medsims_field_val.trim() : ''));
medsims_field_val = this.getValueOf('info', 'medsims_link_expiry');
medsims_val = medsims_val.replace("[medsims_link_expiry]", (medsims_field_val ? medsims_field_val.trim() : ''));
medsims_field_val = this.getValueOf('info', 'medsims_caption_text');
medsims_val = medsims_val.replace('[medsims_caption_text]', (medsims_field_val ? medsims_field_val.trim() : ''));
var text = CKEDITOR.tools.trim(medsims_val);
CKEDITOR.plugins.link.getSelectedLink(editor).setText(text);
Any help how to get the saving data plain text ?
For printing content that contains those links find template where that text is printed and print it through strip_tags() php funciton to filter out the tags.
And you can configure widget type of that field - change text format to plain text. Or even create your on text format, specify which that you want to keep and which to remove and use that filter for that field. That way newly created node must use your field as you defined, but previously defined won't change their content automatically. To do that:
You could also make some script to read all nodes in content type you are using, read field value, pass it to strip_tags and save it back to database.

Drupal 7 | Preserve file after entity_wrapper unset?

Question is quite simple: How to preserve file on server and in file tables, so its fid is still valid after unsetting/changing value with entity wrapper?
$ewrapper = entity_metadata_wrapper('node', $sourceNode);
unset($sourceNode->field_image[$sourceNode->language][0]);
$ewrapper->save();
Now the related file is deleted as soon as the above is called. Same happends if I use:
$ewrapper->field_image->set($newImage);
In this case I need to keep the old image.
Thanks for your help guys!
I think that you should change file status from FILE_STATUS_TEMPORARY to FILE_STATUS_PERMANENT. Check out this answer here:
https://api.drupal.org/comment/23493#comment-23493
Basically, there is no file_set_status() function, like Drupal 6 had one, but now this code should do the same job:
// First obtain a file object, for example by file_load...
$file = file_load(10);
// Or as another example, the result of a file_save_upload...
$file = file_save_upload('upload', array(), 'public://', FILE_EXISTS_REPLACE);
// Now you can set the status
$file->status = FILE_STATUS_PERMANENT;
// And save the status.
file_save($file);
So, you load file object one or another way, change it's status property and save object back again.

unable to see header after fpdf creates file

I am working on a wordpress plugin,
I want to create a pdf file, For this I used fpdf
By using this code I am able to generate pdf and save it to server
require('fpdf/html_table.php');
$pdf=new PDF_HTML();
$questions = $_POST['question'];
$count = count($questions);
$quests = "";
$pdf->SetFont('times','',12);
$pdf->SetTextColor(50,60,100);
$pdf->AddPage('P');
$pdf->SetDisplayMode(real,'default');
$pdf->SetFontSize(12);
for($i=1;$i<=$count;$i++)
{
$qus_id = $questions[$i-1];
$get_q = "select * from `SelleXam_question` where `id`='$qus_id'";
$get_q = $wpdb->get_results($get_q);
$questf = "Quest $i : ".$get_q[0]->question;
$pdf->Cell(0, 10, $questf."\n");
$pdf->Ln();
}
$dir='C:/wamp/www/';
$filename= "filename.pdf";
$pdf ->Output($dir.$filename);
echo "Save PDF in folder";
But when it saved and displayed the messge Save PDF in Folder. I am unable to see the header part of the wordpress website.
Or when I use
$pdf->Output($filename,'D');
then is there any way that I can show the file in a link
When you are developing for Wordpress, you can't just echo text at any time. Wordpress has a whole series of actions it goes through to progressively generate the output rendered to the browser. If you generate output at an inappropriate time, you'll mess up Wordpress' ability to generate output at the right time.
If I were developing this, I'd have this function be called as a filter on the_content. Your output code would change to something like this:
function append_pdf_to_content($content) {
//Your existing code
$pdf->Output($filename, 'F');
$pdf_link = "<br><a href='$filename' download>Download PDF</a>"
return $content . $pdf_link;
}
add_filter('the_content', 'append_pdf_to_content');
If you wanted to use the D option, you'll need to link your users to a separate php page using a target='_blank' that calls the download. Otherwise the PDFs headers will override any headers Wordpress is trying to send.
BTW, you also might want to also take a look at mPDF, it's a fork of fpdf and remains in active development.

SDL Tridion GetListKeywords using Anquilla Framework

I'm writing a GUI extension and using the Anquilla framework to get a list of Keywords within a Category. I'm obtaining an XML document for the list of keywords then working with that document within my extension.
My problem is that the returned XML doesn't contain the Keyword's 'Description' value. I have the Title and Key etc.
My original code looks like this:
var category = $models.getItem("CATEGORYTCMID:);
var list = category.getListKeywords();
list.getXml();
A typical node returned is this:
<tcm:Item ID="tcm:4-1749-1024"
Type="1024" Title="rate_one" Lock="0" IsRoot="true"
Modified="2012-12-17T23:01:59" FromPub="010 Schema"
Key="rate_one_value" IsAbstract="false"
CategoryTitle="TagSelector"
CategoryID="tcm:4-469-512" Icon="T1024L0P0"
Allow="268560384" Deny="96" IsNew="false"
Managed="1024"/></tcm:ListKeywords>
So I've tried using a Filter to give me additional column information:
var filter = new Tridion.ContentManager.ListFilter();
filter.columns = Tridion.Constants.ColumnFilter.EXTENDED;
var list = category.getListKeywords(filter);
Unfortunately this only gives the additional XML attributes:
IsShared="true" IsLocalized="false"
I'd really like the description value to be part of this XML without having to create a Keyword object from the XML. Is such a thing possible?
cough any ideas? cough
I'm afraid you'll have to load the Keyword itself to get the Description.
It's not used in any lists, so it's not returned in the XML.
You could always create a List Extender to add this information to the list, but try to be smart about it since this extender will execute everytime a GetList is called.
Won't save you from having to open every keyword in the list, but you'll be doing it server-side (with Core Service/NetTcp for instance) which will probably be easier and faster than opening each keyword with Anguilla.
In this instance I only need the one keyword, so I simply get it from the CMS. Getting an object in Anguilla is a bit weird, here's the code:
In your main code area:
var selectedKy = $models.getItem("TcmUriOfKeywordHere");
if (selectedKy.isLoaded()) {
p.selectedKy = selectedKy;
this.onselectedKyLoaded();
} else {
$evt.addEventHandler(selectedKy, "load", this.onselectedKyLoaded);
selectedKy.load();
}
It's worth noting how I store the keyword in the properties of the item, so I can obtain it in the onselectedKyLoaded function
The function called once the item is loaded
ContentBloom.ExampleGuiExtension.prototype.onselectedKyLoaded = function (event) {
var p = this.properties;
var selectedDescription = p.selectedKy.getDescription();
// do what you need to do with the description :)
};
I resolved this, thanks to the answer here: https://stackoverflow.com/a/12805939/1221032 - Cheers Nuno :)

Resources