Bad characters when generating pdf file with Knp Snappy - php

I am using Symfony2. When the pdf file is generated using this code :
public function printAction($id)
{
// initialiser $demande
$html = $this->renderView('PFETimeBundle:Demande:print.html.twig',
array('demande'=> $demande)
);
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="file.pdf"'
)
);
}
I get this content (french characters appear in bad characters) :

try to add the encoding property
'encoding' => 'utf-8',
heres a full copy of my working code, pls note that i pass an options array as second argument to getOutPutFromHtml()
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html, array(
'orientation' => 'landscape',
'enable-javascript' => true,
'javascript-delay' => 1000,
'no-stop-slow-scripts' => true,
'no-background' => false,
'lowquality' => false,
'encoding' => 'utf-8',
'images' => true,
'cookie' => array(),
'dpi' => 300,
'image-dpi' => 300,
'enable-external-links' => true,
'enable-internal-links' => true
)),
200,
array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="report.pdf"'
)
);

If you are using the generateFromHtml method, you have to use it like this, at third parameter:
$this->container->get('knp_snappy.pdf')->generateFromHtml(
$this->container->get('templating')->render(
'YourBundle:Template:pdfTemplate.html.twig',
array(
'var' => $var,
)
),
'/path/to/file.pdf',
array(
'encoding' => 'utf-8',
)
);

Related

How to use API in wordpress

I would like to connect one Razorpay Payment API with WordPress, API has authentication token using username and password.
is there any builtin functionality available in WordPress to make a call and handle response?
you can use wp_remote_get()
for example
wp_remote_get( 'http://www.example.com/index.php?action=foo', array( 'timeout' => 120, 'httpversion' => '1.1' ) );
you can also control all request parameters like headers and body data.
Default Usage
global $wp_version;
$args = array(
'timeout' => 5,
'redirection' => 5,
'httpversion' => '1.0',
'user-agent' => 'WordPress/' . $wp_version . '; ' . home_url(),
'blocking' => true,
'headers' => array(),
'cookies' => array(),
'body' => null,
'compress' => false,
'decompress' => true,
'sslverify' => true,
'stream' => false,
'filename' => null
);
Reference : More info

Dompdf package for Laravel 4 return 500 when many ( >9 ) pages are generated

I'm using this Dompdf package for Laravel 4 by Jonathan Thuau
and I'm getting server error whenever I try to generate more than 9 gapes otherwise it works.
Below is the screenshot of the error
Below is also my config file , let me know if I have to change anything in that
return array(
'DOMPDF_TEMP_DIR' => sys_get_temp_dir(),
'DOMPDF_UNICODE_ENABLED' => true,
'DOMPDF_PDF_BACKEND' => 'CPDF',
'DOMPDF_DEFAULT_MEDIA_TYPE' => 'screen',
'DOMPDF_DEFAULT_PAPER_SIZE' => 'letter',
'DOMPDF_DEFAULT_FONT' => 'serif',
'DOMPDF_DPI' => 96,
'DOMPDF_ENABLE_PHP' => false,
'DOMPDF_ENABLE_REMOTE' => false,
'DOMPDF_ENABLE_CSS_FLOAT' => false,
'DOMPDF_ENABLE_JAVASCRIPT' => true,
'DEBUGPNG' => false,
'DEBUGKEEPTEMP' => false,
'DEBUGCSS' => false,
'DEBUG_LAYOUT' => false,
'DEBUG_LAYOUT_LINES' => true,
'DEBUG_LAYOUT_BLOCKS' => true,
'DEBUG_LAYOUT_INLINE' => true,
'DOMPDF_FONT_HEIGHT_RATIO' => 1.1,
'DEBUG_LAYOUT_PADDINGBOX' => true,
'DOMPDF_ENABLE_HTML5PARSER' => false,
'DOMPDF_ENABLE_FONTSUBSETTING' => false,
'DOMPDF_ADMIN_USERNAME' => 'user',
'DOMPDF_ADMIN_PASSWORD' => 'password',
);
The error was due to resource exhaustion and I have to add to following lines on top of the page and it worked
set_time_limit(0);
ini_set("memory_limit",-1);
ini_set('max_execution_time', 0);

Allowing YouTube embed with HTMLPurifier on Laravel 4 and mewebstudio/Purifier

I'm using the mewebstudio/Purifier package service provider HTMLPurifier on Laravel 4.2.
For some reason I can't make the youtube iframe feature work. I even tried the author example for enabling it with no luck.
This is my HTMLPurifier bundle config:
return array(
'encoding' => 'UTF-8',
'finalize' => true,
'preload' => false,
'settings' => array(
'default' => array(
'HTML.Doctype' => 'XHTML 1.0 Strict',
'HTML.Allowed' => 'div[style],b,strong,i,em,a[href|title|style],ul,ol,li,p[style],br,span[style],
img[width|height|alt|src],h1[style],h2[style],h3[style],h4[style],h5[style],table[class|style|summary],tr,td[abbr],tbody,thead',
'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align',
'HTML.SafeObject' => true,
'Output.FlashCompat' => true,
'HTML.SafeIframe' => true,
'URI.SafeIframeRegexp' => '%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%',
'AutoFormat.AutoParagraph' => true,
'AutoFormat.RemoveEmpty' => true,
'HTML.Nofollow' => true,
'URI.Host' => 'domain.com',
),
),
);
And this is my test in routes:
Route::get('/clean', function() {
$pured = Purifier::clean('<iframe src="//www.youtube.com/embed/gLUdqi8J0mQ" width="640" height="360" frameborder="0"></iframe>', 'default');
return $pured;
});
Everything else works, but this.
Thanks fellow warriors :)
HTMLPurifier already has a filter ready-made for Youtube videos, make sure you use it.
To use it make sure you have this line on your config:
'Filter.YouTube' => true
Your final config file would look like this:
return array(
'encoding' => 'UTF-8',
'finalize' => true,
'preload' => false,
'settings' => array(
'default' => array(
'HTML.Doctype' => 'XHTML 1.0 Strict',
'HTML.Allowed' => 'div[style],b,strong,i,em,a[href|title|style],ul,ol,li,p[style],br,span[style],
img[width|height|alt|src],h1[style],h2[style],h3[style],h4[style],h5[style],table[class|style|summary],tr,td[abbr],tbody,thead',
'CSS.AllowedProperties' => 'font,font-size,font-weight,font-style,font-family,text-decoration,padding-left,color,background-color,text-align',
'HTML.SafeObject' => true,
'Output.FlashCompat' => true,
'HTML.SafeIframe' => true,
'URI.SafeIframeRegexp' => '%^(http://|https://|//)(www.youtube.com/embed/|player.vimeo.com/video/)%',
'AutoFormat.AutoParagraph' => true,
'AutoFormat.RemoveEmpty' => true,
'HTML.Nofollow' => true,
'URI.Host' => 'domain.com',
'Filter.YouTube' => true
),
),
);

Elfinder AJAX - Data is not JSON

I am currently getting the folling error when trying to upload an image in Elfinder:
Invalid backend response. Data is not JSON.
I am 95% sure is is a ajax issue as when I take the code out of the switch it works just fine. Even in the switch I am able to view all files and directories but uploads are still an issue
Here is my PHP:
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'))
{
$admin = new systemAdmin;
$response = array('ok' => 0);
$type = (isset($_GET['type'])) ? $_GET['type'] : '';
switch ($type)
{
case 'connector':
if(in_array('filemanager',$admin->user_auth['authority']))
{
include_once($admin->get_setting(5) . '/classes/core/class.elFinderConnector.php');
include_once($admin->get_setting(5) . '/classes/core/class.elFinder.php');
include_once($admin->get_setting(5) . '/classes/core/class.elFinderVolumeDriver.php');
include_once($admin->get_setting(5) . '/classes/core/class.elFinderVolumeLocalFileSystem.php');
$opts = array(
'locale' => 'en_US.UTF-8',
'roots' => array(
array(
'driver' => 'LocalFileSystem',
'path' => $admin->get_setting(5) . '/media/',
'startPath' => $admin->get_setting(5) . '/media/',
'URL' => $admin->get_setting(2) . '/media/',
'treeDeep' => 3,
'alias' => 'Filemanager',
'mimeDetect' => 'internal',
'tmbPath' => '_thumbs',
'utf8fix' => true,
'tmbCrop' => true,
'tmbBgColor' => 'transparent',
'accessControl' => 'access',
'acceptedName' => '/^[^\.].*$/',
'attributes' => array(
array(
'pattern' => '/\.js$/',
'read' => false, 'write' => false, 'hidden' => true, 'locked' => true
),
array(
'pattern' => '/\.htaccess$/',
'read' => false, 'write' => false, 'hidden' => true, 'locked' => true
),
array(
'pattern' => '/_thumbs/',
'read' => false, 'write' => false, 'hidden' => true, 'locked' => true
),
array(
'pattern' => '/featured/',
'read' => false, 'write' => false, 'hidden' => true, 'locked' => true
)
)
),
)
);
header('Access-Control-Allow-Origin: *');
$connector = new elFinderConnector(new elFinder($opts), true);
$connector->run();
}
break;
}
ob_clean();
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
echo json_encode($response);
}
And here is my JS:
var funcNum = getUrlParam('CKEditorFuncNum');
var elf = $('#fileManager').elfinder({
commandsOptions : {
getfile : {
onlyURL : true,
multiple : false,
folders : false,
oncomplete : ''
}
},
height: '488',
url : '/ajax.html?type=connector',
getFileCallback : function(getfile) {
window.opener.CKEDITOR.tools.callFunction(funcNum, getfile);
window.close();
},
resizable: false
}).elfinder('instance');

Zend Cache Front end

I'm using this function in Bootstrap.php to cache my controllers , and i need to cache some controllers only I don't need to cache index controller and article controller as an example
and i need to cache question controller but it does not working . this function is cached all my controllers
protected function _initCache()
{
mb_internal_encoding("UTF-8");
$dir = "/var/www/data/cache/all";
$frontendOptions = array(
'lifetime' => 3600,
'content_type_memorization' => true,
'default_options' => array(
'cache' => true,
'cache_with_get_variables' => true,
'cache_with_post_variables' => true,
'cache_with_session_variables' => true,
'cache_with_cookie_variables' => true,
),
'regexps' => array(
'^/$' => array('cache' => false),
"^/question/" => array('cache' => true),
"^/article/" => array('cache' => false),
)
);
$backendOptions = array(
'cache_dir' =>$dir
);
// getting a Zend_Cache_Frontend_Page object
$cache = Zend_Cache::factory('Page',
'File',
$frontendOptions,
$backendOptions);
$cache->start();
}
so what i can do I tried all soluations please help me.
Thanks
Create a controller plugin with that code, and detect the request.
Something like...
if($request->getControllerName() == 'index' || ... == 'article') {
return;
}
mb_internal_encoding("UTF-8");
...

Categories