Skip to content

Commit

Permalink
Merge pull request #24 from Tazzios/new-pdfjs-parameters
Browse files Browse the repository at this point in the history
New pdfjs parameters #21
  • Loading branch information
Tazzios authored Apr 10, 2023
2 parents eb692ce + 4920023 commit 64c3a54
Show file tree
Hide file tree
Showing 9 changed files with 333 additions and 145 deletions.
11 changes: 11 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
V1.3.0Beta april 2023
A lot of code it rewritten to add the additional parameters. Thats why there is a beta release first.

Content plugin
New: additional parameters (zoom ,pagemode, Search as phrase, nameddest)
Update: PDFjs to v3.5.141

editor button
New: additional parameters
New: default option.

V1.2.6 febrauri 2023
PDFjs has now some mark and comments options.

Expand Down
39 changes: 30 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,12 @@ Compared to other existing pdfviewer for joomla this one is focused on integrati
Show jdownloads file with ID 4.
>{pdfviewer jdownloadsid=4 }
Link to a pdf file
>{pdfviewer file=https://domain.com/file.pdf }
Link to a pdf file on other domain
>{pdfviewer file=https://domain.com/documents/file.pdf }
Link to a pdf file relative to domain name
>{pdfviewer file=/documents/file.pdf }

### Basic Jdownloads example

Expand Down Expand Up @@ -73,16 +77,27 @@ Select viewer. Show the full pdf or only one page as an image.
Only jdownloads pdf files can be shown as image. When set to pdfimage by default the first page will beshown set page= to show an other page. Warning; Images will be created with imagick each time the page is loaded.

PDfjs show the page on default with auto size but you can change that with the following parameter
>zoom=[page-width|page-height|page-fit|auto] (default)
PDfjs show nothing by default on the left side but it can be usefull to show the bookmarks for example
>pagemode=[thumbs|bookmarks|attachments|none] (default) )
Open on specific page
>page=[integer]
Page will be ignore if there is a smartsearch highlight present in the url.
Use ctrl+f5 to test it, else it will remember the old pagenumber.

Link to a page can also with named destination (not a bookmark) but is untested
>nameddest=[destination name]
Highlight keywords
>Search="[keyword1] [keyword2]"
>search="[keyword1] [keyword2]"
keyword will be highlighted separately. If you want to search a combination enable phrase.

double qoutes are only needed with multiple keywords. Each keyword will be highlighted separately.
Enable phrase to search for a combination of words.
>phrase=true
Override default preview style
>style=[embed|popup|new]
Expand All @@ -105,15 +120,21 @@ If you want to link to a webpage with a pdffile embedded you can set following p
>?page=[integer]
>?search=[keyword1]%20[keyword2]
>?search=[keyword1]%20[keyword2]&phrase=true
These url parameters do not work with the pdfimage viewer else everone could access every page of the pdf file.

page and search parameters priority order:
1 highlight search
2 url search
3 url page
4 param search
5 param page
1 highlight search (joomla smartsearch)
2 url search
3 url namedest
4 url page
5 param search
6 param namedest
7 param page

zoom, pagemode and nameddest parameters can also be used. if you add multiple urlparameters think about the syntacs '?' (for the first) en '&' for the others.
>?page=10&pagemode=bookmarks&zoom=page-height

## Make pdf 'searchable'
Expand Down
189 changes: 131 additions & 58 deletions content_plugin/pdfviewer.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,61 +105,143 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)
}
$viewer = strtolower($viewer); // to lower to avoid mis match


/*page and search priority
1 highlight search
2 url search
3 url page
4 param search
5 param page
*/

// get the smartsearch from the url if exist
$search ='';
$pagereference = ''; //value that returns the pageref at the end off this if
$pdfjsviewsettings = ''; //returns pdfjs viewer settings
$pagenumber= '';

// do not process search when pdfimage
if ($viewer<>'pdfimage') {
// only needed when pdfjs
if ($viewer=='pdfjs') {

// get search from url
if (isset($_GET["search"]) ) {
$search = '#search=' . $_GET["search"];
}


// get the parameters from the url if exist
$search ='';
$nameddest ='';


/*page, search and nameddest order priority
1 highlight search
2 url search
3 url namedest
4 url page
5 param search
6 param nameddest
7 param page
*/


// GEt search from joomla smart search
// 1. Get search from joomla smart search
if (isset($_GET["highlight"])) {
$search= base64_decode(htmlspecialchars($_GET["highlight"]));
$search= str_replace('[', '' , $search);
$search= str_replace(']', '' , $search);
$search= str_replace('"', '' , $search);
$search= str_replace(',', ' ' , $search);
$search = '#search=' . $search ;
}

//get searchterm from tagparameters if not set yet and no page by url is given
if ( $search =='' and isset($tagparameters['search']) and trim($tagparameters['search'],'"') <>'' and isset($_GET["page"])==false) {
$pagereference = '#search=' . $search ;
}
//2. get search from url
elseif (isset($_GET["search"]) ) {
$pagereference = '#search=' . $_GET["search"];
}
//3. get nameddest from url
elseif (isset($_GET["nameddest"]) ) {
$pagereference= $_GET["nameddest"];
}
//4. get page from url
elseif (isset($_GET["page"]) ) {
$pagereference= '#page='.$_GET["page"];
}
//5. get searchterm from tagparameters if not set yet by url
elseif (isset($tagparameters['search']) and trim($tagparameters['search'],'"') <>'' ) {
$search = str_replace('%20', ' ' ,$tagparameters['search']); //replace dummy space
$search = trim($search);
$search = trim($search,'"'); // any combination of ' and "
$search = '#search=' . $search ;
$pagereference = '#search=' . $search ;
}
//6. tagparameters nameddest
elseif (isset($tagparameters['nameddest']) ) {
$pagereference = '#nameddest='.trim($tagparameters['nameddest']);
}
//7.get page from tagparameters if no other page redirect is set
elseif (isset($tagparameters['page']) and $tagparameters['page']<>0) {
$pagereference = '#page='.trim($tagparameters['page']);
}

// get search phrase or seperate words: true/false(default)
// only usefull if search was used
if ($search<>''){
$phrase = $this->params->get('phrase');
if (isset($_GET["phrase"]) ) {
$phrase= '&phrase='.$_GET["phrase"];
}
elseif (isset($tagparameters['phrase']) ) {
$phrase = '&phrase='.trim($tagparameters['phrase']);
}
$pagereference .= $phrase;
}


// get page from url
if (isset($_GET["page"]) and $search =='' ) {
$pagenumber= $_GET["page"];
// get zoom url: page-width,page-height,page-fit,auto(default)
if (isset($_GET["zoom"]) ) {
if ($pagereference=='') {
$pdfjsviewsettings = '#zoom='.$_GET["zoom"];
}
else {
$pdfjsviewsettings = '&zoom='.$_GET["zoom"];
}
}
}
// get zoom tagparameter: page-width,page-height,page-fit,auto(default)
if (isset($tagparameters['zoom']) ) {
if ($pagereference=='') {
$pdfjsviewsettings .= '#zoom='.trim($tagparameters['zoom']);
}
else {
$pdfjsviewsettings .= '&zoom='.trim($tagparameters['zoom']);
}
}

// get Pagemode, left sidebar: thumbs,bookmarks,attachments, none (default)
if (isset($_GET["pagemode"]) ) {
if ($pagereference=='' and $pdfjsviewsettings=='' ) {
$pdfjsviewsettings .= '#pagemode='.$_GET["pagemode"];
}
else {
$pdfjsviewsettings .= '&pagemode='.$_GET["pagemode"];
}
}
elseif (isset($tagparameters['pagemode']) ) {
if ($pagereference=='' and $pdfjsviewsettings=='' ) {
$pdfjsviewsettings .= '#pagemode='.trim($tagparameters['pagemode']);
}
else {
$pdfjsviewsettings .= '&pagemode='.trim($tagparameters['pagemode']);
}
}
else{
//prevents that a following viewer on the same page grabs the pagemode from the prvious one.
if ($pagereference=='' and $pdfjsviewsettings=='' ) {
$pdfjsviewsettings .= '#pagemode=none';
}
else {
$pdfjsviewsettings .= '&pagemode=none';
}
}

//get page from tagparameters if not set yet
if (isset($tagparameters['page']) and $pagenumber =='' and $search =='' and $tagparameters['page']<>0) {
$pagenumber = $tagparameters['page'];

}
elseif ($viewer=='pdfimage') {
//set page to create an image from
if ( $pagenumber =='' and isset($tagparameters['page']) and $tagparameters['page']<>0) {
$pagenumber = trim($tagparameters['page']);
}
}




//style
$style = $this->params->get('style');
if (isset($tagparameters['style']) ) {
$style = $tagparameters['style'];
$style = trim($tagparameters['style']);
}
$style = strtolower($style); // to lower to avoid mis match

Expand All @@ -170,8 +252,7 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)
$linktext = str_replace('%20',' ', $tagparameters['linktext']); //replace dummy space back to space
$linktext = trim($linktext,'"'); // any combination of ' and "
}



//PDF viewer size settings:
$height = '' ;
$width = '';
Expand All @@ -190,10 +271,10 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)

// get settings from tag if present
if (isset($tagparameters['height']) ) {
$height = $tagparameters['height'];
$height = trim($tagparameters['height']);
}
if (isset($tagparameters['width']) ) {
$width = $tagparameters['width'];
$width = trim($tagparameters['width']);
}


Expand All @@ -203,19 +284,14 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)
if ( isset($tagparameters['jdownloadsid']) ) {
$path= JPATH_ROOT . '/administrator/components/com_jdownloads';
if (file_exists( $path )) {
$jdownloadsid = $tagparameters['jdownloadsid'];
$jdownloadsid = trim($tagparameters['jdownloadsid']);
$filelink = JUri::base().'index.php?option=com_jdownloads&task=download.send&id='. $jdownloadsid ;
} else {
$showpdfpreview ='no';
$output = "jdownloads is not installed (anymore)";
}
} elseif ( isset($tagparameters['file']) ) {
if (substr($tagparameters['file'], 0,1)=='/'){
$filelink = JUri::base().$tagparameters['file'];
}
else{
$filelink = $tagparameters['file'];
}
$filelink = trim($tagparameters['file']);
}

IF ($showpdfpreview=='yes') {
Expand All @@ -242,25 +318,27 @@ public function onContentPrepare($context, &$article, &$params, $page = 0)
break;*/
default:
// Default pdfjs
$output = CreatePdfviewer($filelink,$search,$pagenumber,$height,$width,$style,$linktext);
$output = CreatePdfviewer($filelink,$pagereference,$pagenumber,$pdfjsviewsettings,$height,$width,$style,$linktext);
break;
}

//cleanup before next loop
unset($tagparameters);

}
}

// We should replace only first occurrence in order to allow positions with the same name to regenerate their content:
$article->text = preg_replace("|$match[0]|", addcslashes($output, '\\$'), $article->text, 1);

//cleanup before next loop
unset($tagparameters,$jdownloadsid,$filelink,$search,$pagenumber,$pdfjsviewsettings,$height,$width,$style,$linktext);

} // end foreach matches

} // end matches
} // end onContentPrepare
}// end class

function CreatePdfviewer($filelink,$search,$pagenumber,$height,$width,$style,$linktext) {
function CreatePdfviewer($filelink,$pagereference,$pagenumber,$pdfjsviewsettings,$height,$width,$style,$linktext) {
// set Path to pdfjs viewer.html file and check if there is an override

//Set default path
Expand All @@ -283,11 +361,6 @@ function CreatePdfviewer($filelink,$search,$pagenumber,$height,$width,$style,$li
$filelink = urlencode($filelink);


if ($pagenumber<>''){
$pagenumber = '#page=' . (int) $pagenumber;
}


//PDF viewer embed settings:
IF ($style=='embed') {

Expand All @@ -299,18 +372,18 @@ function CreatePdfviewer($filelink,$search,$pagenumber,$height,$width,$style,$li
} else {
$width = 'width:' .$width. ';';
}
return '<iframe src="' . $Path_pdfjs . '?file=' . $filelink . $search . $pagenumber . '" style="'.$width.$height.'" frameborder=0> </iframe>';
return '<iframe src="' . $Path_pdfjs . '?file=' . $filelink . $pagereference . $pdfjsviewsettings . '" style="'.$width.$height.'" frameborder=0> </iframe>';
}
// Popup
IF ($style=='popup') {

JHTML::_('behavior.modal');

return '<a class="modal" rel="{handler: \'iframe\', size: {x:'. $width .', y:'. $height .'}}" /*x is width */ href="'. $Path_pdfjs .'?file='. $filelink . $search . $pagenumber .'">'. $linktext .'</a>';
return '<a class="modal" rel="{handler: \'iframe\', size: {x:'. $width .', y:'. $height .'}}" /*x is width */ href="'. $Path_pdfjs .'?file='. $filelink . $pagereference . $pdfjsviewsettings .'">'. $linktext .'</a>';
}
// New window
IF ($style=='new') {
return '<a class="pdfviewer_button" target=_blank href="'. $Path_pdfjs .'?file='. $filelink . $search . $pagenumber .'">'. $linktext .'</a>';
return '<a class="pdfviewer_button" target=_blank href="'. $Path_pdfjs .'?file='. $filelink . $pagereference . $pdfjsviewsettings .'">'. $linktext .'</a>';
}

}
Expand Down Expand Up @@ -370,7 +443,7 @@ function Createpdfimage($file_id,$pagenumber,$height,$width,$style,$linktext) {

//this must be called before reading the image, otherwise has no effect - &quot;-density {$x_resolution}x{$y_resolution}&quot;
//this is important to give good quality output, otherwise text might be unclear
$imgk->setResolution(200,200);
$imgk->setResolution(150,150);

//read the pdf
try {
Expand All @@ -382,7 +455,7 @@ function Createpdfimage($file_id,$pagenumber,$height,$width,$style,$linktext) {

//reduce the dimensions - scaling will lead to black color in transparent regions
IF ($style=='popup') {
$imgk->scaleImage($width-18,0); // -18 to prevent vertical scroll when zoomed in.
$imgk->scaleImage($width-40,0); // -40 to prevent vertical scroll when zoomed in.
} else {
$imgk->scaleImage(1000,0); //only testes with a4 pfds
}
Expand Down
Loading

0 comments on commit 64c3a54

Please sign in to comment.