I am using latest version of TCPDF inorder to generate my PDF files and it is working fine
the problem where I stuck is I want to display file attachment annotation pane by default as it opens when user clicks on attachment image ...
I have tried the following :
$pdf->SetDisplayMode($zoom, $layout, $mode='UseAttachments');
but its not working.
Tell me how it is possible please ....
In my version of TCPDF, SetDisplayMode function looks like this:
public function SetDisplayMode($zoom, $layout='SinglePage', $mode='UseNone') {
if (($zoom == 'fullpage') OR ($zoom == 'fullwidth') OR ($zoom == 'real') OR ($zoom == 'default') OR (!is_string($zoom))) {
$this->ZoomMode = $zoom;
} else {
$this->Error('Incorrect zoom display mode: '.$zoom);
}
switch ($layout) {
//layout is set here
}
//page mode
switch ($mode) {
case 'UseNone': {
$this->PageMode = 'UseNone';
break;
}
case 'UseOutlines': {
$this->PageMode = 'UseOutlines';
break;
}
case 'UseThumbs': {
$this->PageMode = 'UseThumbs';
break;
}
case 'FullScreen': {
$this->PageMode = 'FullScreen';
break;
}
case 'UseOC': {
$this->PageMode = 'UseOC';
break;
}
case '': {
$this->PageMode = 'UseAttachments';
break;
}
default: {
$this->PageMode = 'UseNone';
}
}
}
Take a look at the last case. It will set a PageMode to "UseAttachments", but it looks to be defined in wrong way. Instead of case 'UseAttachments': there is case '':.
So, try to change your code to this:
$pdf->SetDisplayMode($zoom, $layout, '');
Related
I have 4 parameter in my URL. I retrieve the url parameter from my url that is given. With every parameter I'm changing the path to a directory and get different images.
My sample url look like this:
www.sample.com?cat=section1&language=de&prices=pl
The code is working but it's a spagheti code.
Is there a solution to make is less DRY ? How do I retrieve multiple url parameter ?
if(isset($_GET["cat"])) {
switch ($cat) {
case 'section1':
if(isset($_GET["language"])) {
$language = htmlspecialchars($_GET["language"]);
if($language == "de") {
if(isset($_GET["prices"])) {
$prices = htmlspecialchars($_GET["prices"]);
if($prices == "pl"){
$files=glob('pages/section1/dp/low/*.jpg');
}
else {
$files=glob('pages/section1/dn/low/*.jpg');
}
}
else {
$files=glob('pages/section1/dn/low/*.jpg');
}
}
elseif ($language == "en") {
if(isset($_GET["prices"])) {
$prices = htmlspecialchars($_GET["prices"]);
if($prices == "pl"){
$files=glob('pages/section1/ep/low/*.jpg');
}
else {
$files=glob('pages/section1/en/low/*.jpg');
}
}
else {
$files=glob('pages/section1/en/low/*.jpg');
}
}
elseif ($language == "cz") {
if(isset($_GET["prices"])) {
$prices = htmlspecialchars($_GET["prices"]);
if($prices == "pl"){
$files=glob('pages/section1/cp/low/*.jpg');
}
else {
$files=glob('pages/section1/cn/low/*.jpg');
}
}
else {
$files=glob('pages/section1/cn/low/*.jpg');
}
}
else {
$files=glob('pages/section1/cn/low/*.jpg');
}
}
else {
$files=glob('pages/section1/dn/low/*.jpg');
}
break;
case 'section2':
//the same like in section 1, path is .../section2/...
break;
case section3:
//the same like in section 1, path is .../section3/...
break;
default:
//the same like in section 1
break;
}
else {
//the same like in section 1
}
The path d=german, e=english, c=czech, p=prices, n=noprices
You could shorten/remove many if else statements with just doing the checks:
$lang_code = $language[0];
There you have your first letter, you can do the same with every GET parameter.
So you can use that as in:
$files=glob('pages/section1/'.$lang_code.'p/low/*.jpg');
You can do the same for everything else.
P.s.: don't forget to sanitze any user input i.e.:
$language=mysqli_real_escape_string($conn, $_GET['language']);
I'd probably do something like this:
<?php
$allowedCat = ['section1', 'section2'];
$allowedLanguage = ['pl', 'en', 'cz'];
$allowedPrice = ['pl', '??'];
$cat = (isset($_GET["cat"])) ? $_GET["cat"] : null;
$language = (isset($_GET["language"])) ? $_GET["language"] : null;
$prices = (isset($_GET["prices"])) ? $_GET["prices"] : null;
if (!in_array($cat, $allowedCat)) throw new \Exception('Invalid `cat`');
if (!in_array($language, $allowedLanguage)) throw new \Exception('Invalid `language` option.');
if (!in_array($prices, $allowedPrice)) throw new \Exception('Invalid `price` option.');
$someVar1 = ($prices === 'pl') ? 'p' : 'n';
$someVar2 = $language[0];
$files = glob("pages/{$cat}/{$someVar1}{$someVar2}/low/*.jpg");
Think that should be self explanatory. Translates one to one really. Was not certain on how the other price option was specified...
The shortcode I'm trying to create should return the 1 Hungarian word described below, this should change month to month. The code has been placed into functions.php and returns nothing when the shortcode is used in a page or post. Any help would be much appreciated. It does work when used as simple php and html by using echo honapnev($honap);.
$honap = date("n");
function honapnev_shortcode( $ho ) {
if ($ho==1) {
return "Januári";
}
elseif ($ho==2) {
return "Februári";
}
elseif ($ho==3) {
return "Márciusi";
}
elseif ($ho==4) {
return "Áprilisi";
}
elseif ($ho==5) {
return "Májusi";
}
elseif ($ho==6) {
return "Júniusi";
}
elseif ($ho==7) {
return "Júliusi";
}
elseif ($ho==8) {
return "Augusztusi";
}
elseif ($ho==9) {
return "Szeptemberi";
}
elseif ($ho==10) {
return "Októberi";
}
elseif ($ho==11) {
return "Novemberi";
}
elseif ($ho==12) {
return "Decemberi";
}
}
add_shortcode( 'honapnev', 'honapnev_shortcode' );
When I remove the line add_shortcode( 'honapnev', 'honapnev_shortcode' );
the shortcode in brackets appears on the page...
function honapnev_shortcode($atts)
{
switch (date("n"))
{
case 1:
{
$month="Januári";
break;
}
case 2:
{
$month="Februári";
break;
}
case 3:
{
$month="Márciusi";
break;
}
case 4:
{
$month="Áprilisi";
break;
}
case 5:
{
$month="Májusi";
break;
}
case 6:
{
$month="Júniusi";
break;
}
case 7:
{
$month="Júliusi";
break;
}
case 8:
{
$month="Augusztusi";
break;
}
case 9:
{
$month="Szeptemberi";
break;
}
case 10:
{
$month="Októberi";
break;
}
case 11:
{
$month="Novemberi";
break;
}
case 12:
{
$month="Decemberi";
break;
}
default:
{
$month="Januári";
break;
}
}
return $month;
}
add_shortcode('honapnev', 'honapnev_shortcode');
or
function honapnev_shortcode($atts)
{
$months=array("Januári", "Februári", "Márciusi", "Áprilisi", "Májusi","Júniusi", "Júliusi", "Augusztusi","Szeptemberi", "Októberi", "Novemberi", "Decemberi");
return $months[date("n")-1];
}
add_shortcode('honapnev', 'honapnev_shortcode');
You need to parse the shortcode attributes in your function first, also, the elseif or switch statements can be replaced to get a short function.
function honapnev_shortcode($atts)
{
/**
* Get the shortcode attributes default value = 1
*/
$atts = shortcode_atts(array(
'ho' => 1,
), $atts, 'honapnev');
$ho = intval($atts['ho']);
/**
* Is better use an Array in this case
*/
$words = ['Januári', 'Februári', 'Márciusi', 'Áprilisi', 'Májusi', 'Júniusi', 'Júliusi', 'Augusztusi', 'Szeptemberi', 'Októberi', 'Novemberi', 'Decemberi'];
return $words[$ho - 1];
}
add_shortcode('honapnev', 'honapnev_shortcode');
The shortcode
[honapnev]
[honapnev ho="1"]
[honapnev ho="6"]
[honapnev ho="12"]
For example I have this:
switch ($action)
{
case 'my_action':
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
Is the example above going to go through the switch again and then call the first case my_action if the second case second_action has $this_is_true variable set to true?
If this doesn't work, what would be an alternative?
You can try something like this:
switch ($action)
{
case 'my_action':
case 'second_action':
if ($this_is_true || $action=='my_action')
{
doSomething();
}
else
{
doSomethingElse();
}
break;
}
When $action is equal to 'my_action' it will run through the case, as it finds no break sentence then it will run through the second case until it finds the break sentence.
Please give a look to example #3 in http://php.net/manual/en/control-structures.switch.php to find out more about no breaking switch cases.
No it won't. Just call doSomething(); in the 'second_action' case.
I wouldn't use a switch in this simple case, but if it is very long then maybe:
$do = false;
switch ($action)
{
case 'my_action':
$do = true;
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
$do = true;
}
else
{
doSomethingElse();
}
break;
}
if($do) { doSomething(); }
Try to do it this way:
do {
switch ($action)
{
case 'my_action':
$this_is_true = false;
doSomething();
break;
case 'second_action':
if ($this_is_true)
{
$action = 'my_action';
}
else
{
doSomethingElse();
}
break;
}
} while($this_is_true);
Do not forget to switch $this_is_true to false where it needed!
But this code is not beautiful... May be you better to refactor your code.
I created a simple sample php file for display some function outputs. here is the code..
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('Joel');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('Duck');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
function hello($name){
return $name;
}
function pageHeader($header,$onoff){
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
echo $printHeader;
echo $printName;
?>
This code is working fine without any problems.
When I call 'example.com/php/tipo34.php?red', it shows on the screen:
redpage header
Joel
And when I call 'example.com/php/tipo34.php?blue' it shows on the screen:
Duck
I tried to put the below functions inside of another php file called tipo34-req.php and received the following error:
Fatal error: Call to undefined function pageHeader() in C:\wamp\www\php\tipo34.php on line 8
The code I tried:
<?php
// $printName = hello('samitha');
// $printHeader = pageHeader('main','on');
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
include 'tipo34-req.php';
echo $printHeader;
echo $printName;
?>
tipo34-req.php code:
<?php
function hello($name){
global $name;
return $name;
}
function pageHeader($header,$onoff){ global $header, $onoff
if ($onoff == 'on') {
return $header."page header<br>";
}
else {return null;}
}
?>
How do I solve this problem? Using the function directly on the file works, but when I put the functions in another php file, it throws the error.
Thanks.
Include your file above its contents usage. PHP is unaware of the functions since they are included later in the code.
include 'tipo34-req.php';
switch (key($_GET)){
case 'red':
$printHeader = pageHeader('red','on');
$printName = hello('samitha');
break;
case 'blue':
$printHeader = pageHeader('blue','off');
$printName = hello('kalum');
break;
case 'yellow':
//$printHeader = pageHeader('yellow','on');
break;
}
echo $printHeader;
echo $printName;
?>
Have you tried including the file at the top before calling any of the functions?
I have following code:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
break;
}
case (something3):
{
break;
}
}
Also the switch statement must check what one of cases give a TRUE, that is not the problem, the problem is, that i have now a case, where inside of case ... break; after checking other data, i wish to choose other switch-case, the one, that following him.
I have try do this:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(check)
{
something3 = true;
continue;
}
break;
}
case (something3):
{
break;
}
}
But PHP do not wish to go in to the case (something3): its break the full switch statement. How i can pass the rest of code of one case and jump to the next?
This is what's called a "fall-through". Try organizing your code with this concept.
switch (foo) {
// fall case 1 through 2
case 1:
case 2:
// something runs for case 1 and 2
break;
case 3:
// something runs for case 3
break;
}
Using your code:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(check) {
something3 = true;
}
else {
break;
}
}
case (something3):
{
break;
}
}
This will get to case something2 and run your check. If your check passes then it doesn't run the break statement which allows the switch to "fall through" and also do something3.
case (something2):
{
if(!check)
{
break;
}
}
Try this:
switch(true) {
case (something):
{
break;
{
case (something2):
{
if (!check) {
break;
}
}
case (something3):
{
break;
}
}
I had some similar situation and I came up with a solution which in your case would be like this:
switch(true)
{
case (something):
{
break;
}
case (something2):
{
if(!check)
{
break;
}
}
case (something3):
{
break;
}
}
You see that instead of checking the conditions to go to the next case, we checked if we should ignore the current one.