Skip to content

Commit

Permalink
Added: Amazon S3 and Servd compatability
Browse files Browse the repository at this point in the history
  • Loading branch information
bymayo committed Nov 14, 2022
1 parent be4b7a4 commit 9413a24
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 79 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
27 changes: 13 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<img src="https://github.com/bymayo/craft-pdf-transform/blob/craft-4/resources/icon.png" width="60">
<img src="https://github.com/bymayo/craft-pdf-transform/blob/craft-3/resources/icon.png" width="60">

# 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

Expand Down Expand Up @@ -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 <a href="#dimensions">Dimensions</a>.
Expand All @@ -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)

- <a href="https://docs.craftcms.com/v2/image-transforms.html" target="_blank">Image Transforms by Craft</a>
- <a href="https://github.com/aelvan/Imager-Craft" target="_blank">Imager by aelvan</a>
- <a href="https://github.com/nystudio107/craft-imageoptimize" target="_blank">Image Optimize by nystudio107</a>

### 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.
- <a href="https://plugins.craftcms.com/imager-x" target="_blank">Imager X by aelvan</a>
- <a href="https://plugins.craftcms.com/image-optimize" target="_blank">Image Optimize by nystudio107</a>

## 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.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
107 changes: 48 additions & 59 deletions src/services/PdfTransformService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

}

}

Expand Down
21 changes: 16 additions & 5 deletions src/variables/PdfTransformVariable.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

}
}

0 comments on commit 9413a24

Please sign in to comment.