Skip to content

Commit

Permalink
beta test
Browse files Browse the repository at this point in the history
  • Loading branch information
sugeng-sulistiyawan committed Sep 16, 2020
1 parent a51e8de commit 905b5ba
Show file tree
Hide file tree
Showing 10 changed files with 786 additions and 17 deletions.
28 changes: 12 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ Modal::end();

```


- Cara Direct Url
```php
<?php
Expand Down Expand Up @@ -118,9 +117,7 @@ use yii\helpers\Url;
"height" => "360",
"frameborder" => "0",
]
]);

?>
]); ?>

```

Expand All @@ -134,19 +131,18 @@ use yii\helpers\Url;
?>

<?php echo PdfJs::widget([
"url" => Url::to(["/uploads/File.pdf"], true),
"buttons"=>[
"presentationMode" => false,
"openFile" => false,
"print" => true,
"download" => true,
"viewBookmark" => false,
"secondaryToolbarToggle" => false
]
]);
?>
```
"url" => Url::to(["/uploads/File.pdf"], true),
"buttons" => [
"presentationMode" => false,
"openFile" => false,
"print" => false,
"download" => false,
"viewBookmark" => false,
"secondaryToolbarToggle" => false
]
]); ?>

```

- Cara Tambah Watermark
```php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"require": {
"php": ">=7.0",
"yiisoft/yii2": "~2.0",
"bower-asset/pdfjs": "^2.1"
"bower-asset/pdfjs-dist": "^2.5"
},
"require-dev": {
"phpunit/phpunit": "<7"
Expand Down
63 changes: 63 additions & 0 deletions src/Module.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace diecoding\pdfjs;

use yii\helpers\ArrayHelper;

/**
* @inheritdoc
*
* @author Die Coding (Sugeng Sulistiyawan) <[email protected]>
* @copyright 2020 Die Coding
* @license MIT
* @link https://www.diecoding.com
* @version 1.0.0
*/
class Module extends \yii\base\Module
{
/**
*
* @var array $buttons
*/
public $buttons = [];

/**
*
* @var array $waterMark
*/
public $waterMark = [];

/**
* @inheritdoc
*/
public $controllerNamespace = 'diecoding\pdfjs\controllers';

/**
* @inheritdoc
*/
public function init()
{
parent::init();

$waterMarkDefault = [
'text' => '',
'alpha' => '0.5',
'color' => 'red'
];

if (!empty($this->waterMark)) {
$this->waterMark = ArrayHelper::merge($waterMarkDefault, $this->waterMark);
} else {
$this->waterMark = $waterMarkDefault;
}

$this->buttons = ArrayHelper::merge([
'presentationMode' => true,
'openFile' => true,
'print' => true,
'download' => true,
'viewBookmark' => true,
'secondaryToolbarToggle' => true,
], $this->buttons);
}
}
68 changes: 68 additions & 0 deletions src/PdfJs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace diecoding\pdfjs;

use Yii;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\helpers\ArrayHelper;

/**
* @inheritdoc
*
* @author Die Coding (Sugeng Sulistiyawan) <[email protected]>
* @copyright 2020 Die Coding
* @license MIT
* @link https://www.diecoding.com
* @version 1.0.0
*/
class PdfJs extends \yii\base\Widget
{
/**
*
* @var string $url
*/
public $url;

/**
*
* @var array $options
*/
public $options = [];

/**
*
* @var array $buttons
*/
public $buttons = [];

/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->view->registerAssetBundle(PdfJsAsset::className());

$module = new Module();
$buttons = $module->buttons;
$this->buttons = ArrayHelper::merge($buttons, $this->buttons);
}

/**
* @inheritdoc
*/
public function run()
{
if (!array_key_exists('style', $this->options)) {
$this->options['style'] = 'border:solid 2px #404040; width:' . $this->width . '; height:' . $this->height . ';';
}

return $this->render('viewer', [
'options' => $this->options,
'url' => $this->url,
'buttons' => $this->buttons,
'id' => $this->id
]);
}
}
54 changes: 54 additions & 0 deletions src/PdfJsAsset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace diecoding\pdfjs;

use yii\web\AssetBundle;
use yii\web\View;

/**
* @inheritdoc
*
* @author Die Coding (Sugeng Sulistiyawan) <[email protected]>
* @copyright 2020 Die Coding
* @license MIT
* @link https://www.diecoding.com
* @version 1.0.0
*/
class PdfJsAsset extends AssetBundle
{

/**
* @var string $sourcePath
*/
public $sourcePath = '@bower/pdfjs-dist';

/**
* @var array $css
*/
public $css = [
'web/pdf_viewer.css',
];

/**
* @var array $js
*/
public $js = [
'build/pdf.min.js',
// 'build/pdf.worker.min.js',
'web/pdf_viewer.js',
];

/**
* @var array $depends
*/
public $depends = [
'yii\web\YiiAsset',
];

/**
* @var array $jsOptions
*/
public $jsOptions = [
'position' => View::POS_HEAD
];
}
39 changes: 39 additions & 0 deletions src/assets/yii2-pdfjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
function PDFJSIframeform(url) {
var object = this;
object.time = new Date().getTime();
object.form = $(
'<form action="' +
url +
'" target="iframe' +
object.time +
'" method="post" style="display:none;" id="form' +
object.time +
'" name="form' +
object.time +
'"></form>'
);

object.addParameter = function (parameter, value) {
$("<input type='hidden' />")
.attr("name", parameter)
.attr("value", value)
.appendTo(object.form);
};

object.send = function () {
var iframe = $(
'<iframe data-time="' +
object.time +
'" style="display:none;" id="iframe' +
object.time +
'"></iframe>'
);
$("body").append(iframe);
$("body").append(object.form);
object.form.submit();
iframe.load(function () {
$("#form" + $(this).data("time")).remove();
$(this).remove();
});
};
}
53 changes: 53 additions & 0 deletions src/controllers/DefaultController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

namespace diecoding\pdfjs\controllers;

use diecoding\pdfjs\Module;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\Controller;

/**
* @inheritdoc
*
* @author Die Coding (Sugeng Sulistiyawan) <[email protected]>
* @copyright 2020 Die Coding
* @license MIT
* @link https://www.diecoding.com
* @version 1.0.0
*/
class DefaultController extends Controller
{
/**
* @inheritDoc
*/
public $layout = 'main';

/**
* Renders the index view for the module
* @return string
*/
public function actionIndex()
{
$module = new Module();
$buttons = $module->buttons;
$waterMark = $module->waterMark;
if (Yii::$app->request->isPost) {

$widgitButtonConfig = Yii::$app->request->post();
if (isset(Yii::$app->request->csrfParam)) {
unset($widgitButtonConfig[Yii::$app->request->csrfParam]);
}

foreach ($widgitButtonConfig as $key => $value) {
$widgitButtonConfig[$key] = trim($value) == '0' ? false : true;
}
$buttons = ArrayHelper::merge($buttons, $widgitButtonConfig);
}

return $this->render('index', [
'buttons' => $buttons,
'waterMark' => $waterMark,
]);
}
}
Loading

0 comments on commit 905b5ba

Please sign in to comment.