diff --git a/CHANGELOG.md b/CHANGELOG.md
index 83584ff..794b4a7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,11 @@
# PDF Transform Changelog
+## 2.0.1 - 2022-11-14
+### Added
+- Amazon S3 Compatability
+- Servd Compatability (Thanks @mattgrayisok / @servd)
+- New `.render()` method that outputs the transformed image as a Craft asset (No longer just the URL 🎉)
+
## 2.0.0 - 2022-06-17
### Changed
- Now requires PHP ^8.0.0.
diff --git a/README.md b/README.md
index 8ffaca0..d4d2e9a 100644
--- a/README.md
+++ b/README.md
@@ -1,16 +1,16 @@
-
+
# PDF Transform for Craft CMS 4
PDF Transform is a Craft CMS plugin that transforms a PDF stored in Assets, to an image. This can then be output via Twig in to a template.
A use case for this is to show the preview of a PDF before a user downloads that particular file.
-
## Features
- Transform PDF's to images via Twig (The file needs to be an existing Asset element)
- PDF's are transformed to an image when PDF's are uploaded via Assets or Asset fields.
- Transformed PDF images are indexed and available in Assets like all other asset elements.
+- Works with local asset volumes, Amazon S3 and Servd.
## Install
@@ -63,13 +63,18 @@ You can also install the plugin via the Plugin Store in the Craft Admin CP by se
## Templating
-To transform a PDF to an image, and then output the URL use the following Twig tag:
+To transform a PDF to an image use the following Twig tag:
```
-{% set asset = entry.pdfAsset.one() %}
-{{ craft.pdfTransform.url(asset) }}
+{% set pdfToTransform = entry.pdfAsset.one() %}
+
+{% set transformedPdf = craft.pdfTransform.render(pdfToTransform) %}
+
+{{ transformedPdf.one().url }}
```
+The transformed PDF (Now an image stored in Assets) can then be output using `{{ transformedPdf.one().url }}`. Or get any Asset property e.g. `title`, `id`, `filename` etc.
+
If the transformed image doesn't exist then the PDF will be transformed via the template. This may cause the template/page to become slow whilst the PDF is transformed.
Be aware that this also may output a large image, so we'd recommend running this through an image transform. See Dimensions.
@@ -89,20 +94,14 @@ PDF Transform does the basic job of converting your PDF to a single image. It wi
I'd recommend running the PDF image through one of the following options/plugins and setting the dimensions that way (Some of these also handle caching the image as well)
- Image Transforms by Craft
-- Imager by aelvan
-- Image Optimize by nystudio107
-
-### Local
-
-Currently the plugin has only been tested with local assets, not assets through Amazon S3 etc. It may, or may not work with remote assets.
+- Imager X by aelvan
+- Image Optimize by nystudio107
## Support
-If you have any issues (Surely not!) then I'll aim to reply to these as soon as possible. If it's a site-breaking-oh-no-what-has-happened moment, then hit me up on the Craft CMS Slack - @bymayo
+If you have any issues (Surely not!) then I'll aim to reply to these as soon as possible. If it's a site-breaking-oh-no-what-has-happened moment, then hit me up on the Craft CMS Discord - @bymayo
## Roadmap
-- Output asset element, not just the URL so that all Asset methods are available.
- Optional variables (E.g. page, resolution etc)
-- Test and support remote assets.
- When PDF assets are updated, ensure old transformed image is removed.
diff --git a/composer.json b/composer.json
index 33dc9bf..33858a6 100644
--- a/composer.json
+++ b/composer.json
@@ -2,7 +2,7 @@
"name": "bymayo/pdf-transform",
"description": "Transform a PDF page to an image (JPEG, PNG)",
"type": "craft-plugin",
- "version": "2.0.0",
+ "version": "2.0.1",
"keywords": [
"craft",
"cms",
diff --git a/src/services/PdfTransformService.php b/src/services/PdfTransformService.php
index 78e3694..4263330 100644
--- a/src/services/PdfTransformService.php
+++ b/src/services/PdfTransformService.php
@@ -70,87 +70,76 @@ public function getImageFs()
return $fs;
}
- public function getImagePath($asset, $aliasType)
- {
-
- return Yii::getAlias(App::parseEnv($this->getImageFs()->$aliasType)) . '/' . $this->getFileName($asset);
-
- }
-
public function getFileName($asset)
{
// e.g. filename-12345.jpg
return $asset->filename . '-' . $asset->id . '.' . $this->settings->imageFormat;
}
- public function url($asset)
- {
-
- // @TODO: Check to see if asset exists using actual Asset volume path from volume settings
- if (!file_exists($this->getImagePath($asset, 'path')))
- {
-
- $this->pdfToImage(
- $asset
- );
-
- }
-
- // Get Asset Path
- // @TODO: Check to see if asset exists using actual Asset volume path from volume settings
- return $this->getImagePath($asset, 'url');
-
- }
-
- public function getPdfAssetPath($asset)
- {
+ public function render($asset)
+ {
- $volumePath = Yii::getAlias(App::parseEnv($asset->getVolume()->getFs()->path));
- $folderPath = $asset->getPath();
+ $volume = $this->getImageVolume();
+ $fs = $this->getImageFs();
+ $fileName = $this->getFileName($asset);
- $assetPath = $volumePath . '/' . $folderPath;
+ if ($fs->fileExists($fileName)) {
+
+ $transformedAsset = Asset::find()
+ ->volumeId($volume->id)
+ ->filename($fileName)
+ ->one();
- return $assetPath;
+ return $transformedAsset;
- }
+ }
- public function pdfToImage($asset)
- {
+ return $this->pdfToImage(
+ $asset
+ );
- // @TODO: Check to see if file exists
- $pdf = new Pdf($this->getPdfAssetPath($asset));
+ }
- $pdf
- ->setPage($this->settings->page)
- ->setResolution($this->settings->imageResolution)
- ->setCompressionQuality($this->settings->imageQuality)
- ->saveImage($this->getImagePath($asset, 'path'));
+ public function pdfToImage($asset)
+ {
- $this->indexAsset($asset);
+ $filename = $this->getFileName($asset);
+ $volume = $this->getImageVolume();
- return true;
+ $pathService = Craft::$app->getPath();
+ $tempPath = $pathService->getTempPath(true) . '/' . mt_rand(0, 9999999) . '.png';
+ file_put_contents($tempPath, file_get_contents($asset->url));
- }
+ $tempPathTransform = $pathService->getTempPath(true) . '/' . $filename;
- public function indexAsset($asset)
- {
+ $folder = Craft::$app->getAssets()->getRootFolderByVolumeId($volume->id);
- $volume = $this->getImageVolume();
- $fs = $this->getImageFs();
- $fileName = $this->getFileName($asset);
+ $pdf = new Pdf($tempPath);
- if ($fs->fileExists($fileName)) {
+ $pdf
+ ->setPage($this->settings->page)
+ ->setResolution($this->settings->imageResolution)
+ ->setCompressionQuality($this->settings->imageQuality)
+ ->saveImage($tempPathTransform);
- $assetIndexer = Craft::$app->getAssetIndexer();
- $session = $assetIndexer->createIndexingSession([$volume]);
+ $assetTransformed = new Asset();
+ $assetTransformed->tempFilePath = $tempPathTransform;
+ $assetTransformed->filename = $filename;
+ $assetTransformed->folderId = $folder->id;
+ $assetTransformed->newFolderId = $folder->id;
+ $assetTransformed->kind = 'Image';
+ $assetTransformed->title = $asset->title;
+ $assetTransformed->avoidFilenameConflicts = true;
+ $assetTransformed->setVolumeId($volume->id);
+ $assetTransformed->setScenario(Asset::SCENARIO_CREATE);
- Craft::$app->getAssetIndexer()->indexFile(
- $volume,
- $fileName,
- $session->id
- );
+ $assetTransformed->validate();
+
+ if (Craft::$app->getElements()->saveElement($assetTransformed, false))
+ {
+ return $assetTransformed;
+ }
- }
}
diff --git a/src/variables/PdfTransformVariable.php b/src/variables/PdfTransformVariable.php
index f006a4b..131e189 100644
--- a/src/variables/PdfTransformVariable.php
+++ b/src/variables/PdfTransformVariable.php
@@ -26,8 +26,19 @@ class PdfTransformVariable
* @param null $optional
* @return string
*/
- public function url($asset)
- {
- return PdfTransform::$plugin->pdfTransformService->url($asset);
- }
-}
+ public function render($asset)
+ {
+ return PdfTransform::$plugin->pdfTransformService->render($asset);
+ }
+
+ public function url($asset)
+ {
+
+ $render = PdfTransform::$plugin->pdfTransformService->render($asset);
+
+ if ($render) {
+ return $render->getUrl();
+ }
+
+ }
+}
\ No newline at end of file