Skip to content

Commit

Permalink
Merge branch 'release/1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
endelwar committed Jul 17, 2021
2 parents 9bf46a6 + b8e69b5 commit 28a6047
Show file tree
Hide file tree
Showing 31 changed files with 1,253 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.github/ export-ignore
tests/ export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.php-cs-fixer-dist.php export-ignore
phpunit.xml.dist export-ignore
155 changes: 155 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
name: "CI Tests"

on:
pull_request:
push:

jobs:
php-74-symfony-44:
name: PHP 7.4 / Symfony 4.4
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
with:
fetch-depth: 2

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
php-version: "7.4"

- name: "Cache composer packages"
uses: "actions/cache@v2"
with:
path: "~/.composer/cache"
key: "php-7.4-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-7.4-composer-locked-"

- name: "Install Symfony 4.4"
run: "composer require symfony/symfony:4.4.* --no-update"

- name: "Install dependencies with composer"
run: "composer update --no-interaction"

- name: "Run PHPUnit Tests"
run: "composer test"

php-74-symfony53:
name: PHP 7.4 / Symfony 5.3
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
with:
fetch-depth: 2

- name: "Install PHP 7.4"
uses: "shivammathur/setup-php@v2"
with:
php-version: "7.4"

- name: "Cache composer packages"
uses: "actions/cache@v2"
with:
path: "~/.composer/cache"
key: "php-7.4-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-7.4-composer-locked-"

- name: "Install Symfony 5.3"
run: "composer require symfony/symfony:5.3.* --no-update"

- name: "Install dependencies with composer"
run: "composer update --no-interaction"

- name: "Run PHPUnit Tests"
run: "composer test"

php-80-symfony44:
name: PHP 8.0 / Symfony 4.4
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
with:
fetch-depth: 2

- name: "Install PHP 8"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8"

- name: "Cache composer packages"
uses: "actions/cache@v2"
with:
path: "~/.composer/cache"
key: "php-8.0-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-8.0-composer-locked-"

- name: "Install Symfony 4.4"
run: "composer require symfony/symfony:4.4.* --no-update"

- name: "Install dependencies with composer"
run: "composer update --no-interaction"

- name: "Run PHPUnit Tests"
run: "composer test"
php-80-symfony53:
name: PHP 8.0 / Symfony 5.3
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
with:
fetch-depth: 2

- name: "Install PHP 8"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8"

- name: "Cache composer packages"
uses: "actions/cache@v2"
with:
path: "~/.composer/cache"
key: "php-8.0-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-8.0-composer-locked-"

- name: "Install Symfony 5.3"
run: "composer require symfony/symfony:5.3.* --no-update"

- name: "Install dependencies with composer"
run: "composer update --no-interaction"

- name: "Run PHPUnit Tests"
run: "composer test"

php8:
name: PHP 8 / Symfony 5.4@dev
runs-on: ubuntu-latest
steps:
- name: "Checkout"
uses: "actions/checkout@v2"
with:
fetch-depth: 2

- name: "Install PHP 8"
uses: "shivammathur/setup-php@v2"
with:
php-version: "8"

- name: "Cache composer packages"
uses: "actions/cache@v2"
with:
path: "~/.composer/cache"
key: "php-8-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-8-composer-locked-"

- name: "Install Symfony 5.4"
run: "composer require symfony/symfony:5.4.*@dev --no-update"

- name: "Install dependencies with composer"
run: "composer update --no-interaction"

- name: "Run PHPUnit Tests"
run: "composer test"
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
/composer.lock
/.php-cs-fixer.cache
.phpunit.result.cache
20 changes: 20 additions & 0 deletions .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

$finder = (new PhpCsFixer\Finder())
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
;

return (new PhpCsFixer\Config())
->setRiskyAllowed(true)
->setRules([
'@Symfony' => true,
'@Symfony:risky' => true,
'array_syntax' => ['syntax' => 'short'],
'concat_space' => ['spacing' => 'one'],
'cast_spaces' => ['space' => 'none'],
'native_function_invocation' => false,
'no_superfluous_phpdoc_tags' => ['allow_mixed' => true],
])
->setFinder($finder)
;
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Changelog
All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.0.0] - 2021-07-17
### Added
- First public release
137 changes: 137 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
# WeasyPrintBundle
[PhpWeasyPrint](https://github.com/pontedilana/php-weasyprint) is a PHP (7.4+) wrapper for [WeasyPrint](https://weasyprint.org/) PDF generator.
It allows you to generate PDF files from HTML string or URL.

The WeasyPrintBundle provides a simple integration for your Symfony project.

This bundle is massively inspired by [KnpLabs/KnpSnappyBundle](https://github.com/KnpLabs/KnpSnappyBundle), of which it aims to be a one-to-one substitute

## Installation

With [composer](https://getcomposer.org), require:

`composer require pontedilana/weasyprint-bundle`

Then enable it in your kernel (a flex recipe is coming soon):

```php
// config/bundles.php
<?php

return [
//...
Pontedilana\WeasyprintBundle\WeasyprintBundle::class => ['all' => true],
//...
];
```

## Configuration
If you need to change the binaries, change the instance options or even disable one or both services, you can do it through the configuration.

```yaml
# config/packages/weasyprint.yaml
weasyprint:
pdf:
enabled: true
binary: /usr/local/bin/weasyprint
options: []
image:
enabled: true
binary: /usr/local/bin/weasyprint
options: []
```
If you want to change temporary folder which is ```sys_get_temp_dir()``` by default, you can use

```yaml
# config/packages/weasyprint.yaml
weasyprint:
temporary_folder: "%kernel.cache_dir%/weasyprint"
```

You can also configure the timeout used by the generators with `process_timeout`:

```yaml
# config/packages/weasyprint.yaml
weasyprint:
process_timeout: 20 # In seconds
```

## Usage

The bundle registers two services:

- the `weasyprint.pdf` service allows you to generate pdf files.
- the `weasyprint.image` service allows you to generate images (works only with WeasyPrint 52.5 or lower);

### Generate a pdf document from a URL

```php
// @var Pontedilana\PhpWeasyPrint\Pdf
$weasyprintPdf->generate('https://www.github.com', '/path/to/the/file.pdf');
```

### Generate a pdf document from a twig view

```php
// @var Pontedilana\PhpWeasyPrint\Pdf
$weasyprintPdf->generateFromHtml(
$this->renderView(
'frontend/product/pdf.html.twig',
[
'some' => $vars,
]
),
'/path/to/the/file.pdf'
);
```

### Render a pdf document as response from a controller

```php
use Pontedilana\WeasyprintBundle\WeasyPrint\Response\PdfResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class SomeController extends AbstractController
{
public function pdfAction(Pontedilana\PhpWeasyPrint\Pdf $weasyprintPdf)
{
$html = $this->renderView(
'frontend/product/pdf.html.twig',
[
'some' => $vars,
]
);
return new PdfResponse(
$weasyprintPdf->getOutputFromHtml($html),
'file.pdf'
);
}
}
```

### Render a pdf document with a relative url inside like css files or images

```php
use Pontedilana\WeasyprintBundle\WeasyPrint\Response\PdfResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
class SomeController extends AbstractController
{
public function pdfAction(Pontedilana\PhpWeasyPrint\Pdf $weasyprintPdf)
{
$pageUrl = $this->generateUrl('homepage', [], true); // use absolute path!
return new PdfResponse(
$weasyprintPdf->getOutput($pageUrl),
'file.pdf'
);
}
}
```

## Credits

WeasyPrintBundle and [PhpWeasyPrint](https://github.com/pontedilana/php-weasyprint) has been developed by [Pontedilana](https://www.pontedilana.it/).
SnappyBundle has been developed by [KnpLabs](https://knplabs.com).
52 changes: 52 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "pontedilana/weasyprint-bundle",
"type": "symfony-bundle",
"description": "Easily create PDF in Symfony by converting Twig/HTML templates.",
"keywords": [
"weasyprint",
"pdf",
"bundle",
"symfony-bundle"
],
"license": "MIT",
"authors": [
{
"name": "Pontedilana Dev Team",
"homepage": "https://www.pontedilana.it"
}
],
"require": {
"php": "^7.4 || ^8.0",
"pontedilana/php-weasyprint": "^0.9 || ^1.0",
"symfony/framework-bundle": "^4.4 || ^5.3"
},
"require-dev": {
"doctrine/annotations": "^1.11",
"friendsofphp/php-cs-fixer": "^3.0",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12",
"symfony/asset": "^4.4 || ^5.3",
"symfony/finder": "^4.4 || ^5.3",
"symfony/phpunit-bridge": "^4.4 || ^5.3",
"symfony/security-csrf": "^4.4 || ^5.3",
"symfony/templating": "^4.4 || ^5.3",
"symfony/validator": "^4.4 || ^5.3",
"symfony/yaml": "^4.4 || ^5.3"
},
"autoload": {
"psr-4": {
"Pontedilana\\WeasyprintBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Pontedilana\\WeasyprintBundle\\Tests\\": "tests/"
}
},
"scripts": {
"check-cs": "vendor/bin/php-cs-fixer fix --diff --dry-run --verbose",
"fix-cs": "vendor/bin/php-cs-fixer fix --verbose",
"static-analysis": "vendor/bin/phpstan analyse --ansi",
"test": "vendor/bin/simple-phpunit"
}
}
Loading

0 comments on commit 28a6047

Please sign in to comment.