using DomPdf to create PDF from html.
$html = '<html><head><body>Test</body></head></html>
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
$pdf->render();
return $pdf->stream();
Get error:
Call to protected method Barryvdh\DomPDF\PDF::render() from context 'App\Http\Controllers\TestController'
When remove "
$pdf->render();
Create PDF file,but mess html code...
Tnx, P
I'm guessing you're using https://github.com/barryvdh/laravel-dompdf?
You're receiving the error because you're trying to call a protected method from your controller.
The docs say you can do the following:
You can save it to a file, or stream (show in browser) or download.
$pdf->save('myfile.pdf')
$pdf->stream();
$pdf->download('invoice.pdf');
Related
In wordpress site while fetching JSON data using WP REST API how to decode –, “, etc.
Some latest android webview fails to decode it.
Add this code in your theme functions.php , it acts as a middleware to resolve entities before serving it to the API.
function fix_decode_rest_api($response, $post, $request) {
if (isset($post)) {
$decodedTitle = html_entity_decode($post->post_title);
$response->data['title']['rendered'] = $decodedTitle;
$decodedPostTitle = html_entity_decode($response->data['title']['rendered']);
$response->data['title']['rendered'] = $decodedPostTitle;
}
return $response;
}
add_filter('rest_prepare_post', 'fix_decode_rest_api', 10, 3);
Just decode the string in the request instead of calling the database all over again. You already have the content to decode in $response
$response->data['title']['rendered'] = html_entity_decode($response->data['title']['rendered']);
. . . .
I am using Codeigniter 3.1 and PHPExcel 1.8.
I have a function that creates a PHPExcel Object and returns it and the other function outputs the Excel to browser
$objPHPExcel = $this->MyExcelModel->my_function();
$file_name = "my_file.xls";
Content-Type: application/vnd.ms-excel");
Content-Disposition: attachment; filename=$file_name");
Cache-Control: max-age=0");
PHPExcel_IOFactory::createWriter($objPHPExcel, "Excel5");
save("php://output");
This is working perfectly fine. Now I also need the HTML of the same Excel Data. Previously I am creating a separate preview function. But whenever there is a chnage I have to do it in two locations. So I searched and found that I can generate HTML too using PHPExcel. Following is the code that I used
$objPHPExcel = $this->MyExcelModel->my_function();
$objWriter2 = new PHPExcel_Writer_HTML($objPHPExcel);
$html = "";
$html .= $objWriter2->generateHTMLHeader();
$html .= $objWriter2->generateSheetData();
$html .= $objWriter2->generateHTMLFooter();
I am sending this html in json response for an ajax call. When the function is called. I get an error
<h4>A PHP Error was encountered</h4>
<p>Severity: Notice</p>
<p>Message: Undefined property: PHPExcel_Writer_HTML::$_sheetIndex</p>
<p>Filename: Writer/HTML.php</p>
<p>Line Number: 401</p>
and the json response has the html too. So the html is generating but I am getting this error. I have searched a lot but without success. So I have pretty much got what I needed but this error is restricting me from using it. so HELP . . . . .
Go and edit /Classes/PHPExcel/Writer/HTML.php and find line 401, which reads
if ($this->_sheetIndex !== null || !$this->spansAreCalculated) {
change it to read
if ($this->sheetIndex !== null || !$this->spansAreCalculated) {
To fix it immediately
I'll push the fix to the develop branch on github in a few minutes
So I have a wordpress Ajax function that retrieves MySQL data as JSON and logs it. However I want to not directly get the data back on the page where my AJAX function is, but I want to save the data to a JSON file instead, so I can use it for a wider variety of purposes.
Here is my AJAX function:
$.get(
ajax_url,
data,
function(data) { // AJAX callback
fill_json(data);
}// End AJAX callback
);
The fill_json() is a function to echo the JSON data in a table I wrote myself.
Now here is what happens inside my AJAX hook:
$sql_search = $wpdb->get_results(" a complicated mysql search here ");
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$result = json_encode($result);
echo $result;
} else {
header("Location: ".$_SERVER["HTTP_REFERER"]);
}
underneath echo $result; in my Ajax hook I tried the following piece of code, but I don't know how I can see if it worked or not:
$json_path = "/var/www/vhosts/jtc.ae/httpdocs/pre/wp/wp-content/themes/Amazing_japan_HP/new/search.json";
file_put_contents($json_path, $result);
My question:
Is this the correct way to save the data to a JSON file, and how can I get this data on my main page then?
Extra question: Will saving $result to a JSON file conflict with multiple users using the AJAX at the same time?
All you did is right .. please make sure the steps below :
Pass write permission to the folder /new inside "Amazing_japan_HP" (chmod -R 666 /new)
To save the data in file use file_put_contents($file, $current);
To get the data from the file use $current = file_get_contents($file);
For all newly created files try to use umask. It provides required permission to the files for writing content in it
umask() sets PHP's umask to mask & 0777 and returns the old umask.
When PHP is being used as a server module, the umask is restored when
each request is finished.
I intending to get a json response without rendering a view with eZ Publish.
So I trying to use a custom module to do that:
function jsonConvert(){
$articles = eZFunctionHandler::execute(
'content',
'tree',
array(
'parent_node_id' => '59'
)
);
header("Content-Type: application/json");
return json_encode($articles);
}
echo jsonConvert();
How can I compile this module without using a basic URL that rendering a view like domain.com/module/view/ in order to get a json response without any HTML code?
echo json_encode( YOUR ARRAY );
eZExecution::cleanExit();
It's all what you need in your custom module/view php file to return json.
If I were you :
use the builtin feature that allows you to use a different layout to render a content view. Create a new layout 'MYLAYOUT' within a layout.ini.append.php override (see https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Configuration-files/layout.ini) and then call your view using /layout/set/MYLAYOUT/your/content/uri
specify the content type in the layout configuration to match your requirements (application/json as the content type)
create a pagelayout.tpl template used by your newly created layout which basically only contains {$module_result.content}
create a template operator to convert your contents into a 'readable' json and call it from the template rendering your content (probably a /node/view/full.tpl override)
alternative (but not that sexy) to #4 => call json_encode directly in your template by allowing the php function to be called in your templates (see https://doc.ez.no/eZ-Publish/Technical-manual/4.x/Reference/Configuration-files/template.ini/PHP/PHPOperatorList)
To get a blank pagelayout in your module, and set a json content type, you can add this following lines in your module php file :
header("Content-Type: application/json");
$Result = array();
$Result['content'] = json_encode($articles);
$Result['pagelayout'] = false;
I am trying to save HTML content with the Symfony2 form system but I am facing issues with escaping.
My addPost action looks like this
$em = $this->getDoctrine()->getEntityManager();
$post = new Post();
$postForm = $this->createForm(new PostFormType(), $post);
if ($request->getMethod() == 'POST') {
$postForm->bindRequest($request);
if($postForm->isValid()){
$em->persist($post);
$em->flush();
}
}
The problem is that the post form has a content textarea that allows the user to enter html. When the form is submitted with html like test the content gets saved to the database as test. And then on each subsequent save the backslashes escape themselves again and again...
What is the proper way to store HTML with the Symfony2 form component?
You should remove the backslashes from the string before sending it to the user to edit. You should be able to get the content from the $post object and then strip its slashes and set it back to the post content like so.
$post = new Post();
$post->setContent(stripslashes($post->getContent()));
$postForm = $this->createForm(new PostFormType(), $post);
Now when the form renders it will not have the escape characters.
However, I have found that if you return a response to the same page you will still see the backslashes. To avoid this you can return a RedirectResponse like this
if($postForm->isValid()){
$em->flush();
return new RedirectResponse('route', array('postId' => $postId)));
}
Those slashes are placed there so that the mysql engine doesn't confuse those quotes with sql syntax. I suggest you use a rejex to get rid of all the slashes when you retrieve the field from the database, but before you post it to the client. let me see if I can find the rejex for you, maybe someone will beat me to it :)