Skip to content

Commit

Permalink
Merge branch 'release/0.9.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
endelwar committed Jul 16, 2021
2 parents 3bc31d0 + 24de26d commit b0191db
Show file tree
Hide file tree
Showing 22 changed files with 2,318 additions and 0 deletions.
51 changes: 51 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: CI
on:
- push
- pull_request
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
php-version:
- "7.4"
- "8.0"

name: PHP ${{ matrix.php-version }}

steps:
- name: Checkout
uses: actions/checkout@v2

- name: Setup PHP, with composer and extensions
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-version }}
tools: phpstan

- name: Setup problem matchers for PHP
run: echo "::add-matcher::${{ runner.tool_cache }}/php.json"

- name: Setup problem matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"


- name: Get composer cache directory
id: composer-cache
run: echo "::set-output name=dir::$(composer config cache-files-dir)"

- name: Cache dependencies
uses: actions/cache@v2
with:
path: ${{ steps.composer-cache.outputs.dir }}
key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}
restore-keys: ${{ runner.os }}-composer-

- name: Install dependencies
run: composer install --prefer-dist

- name: Test with PHPUnit
run: ./vendor/bin/phpunit

- name: Run PHPStan
run: phpstan analyse src
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/vendor/
/.phpunit.cache/
/.infection/
/.php-cs-fixer.cache
/composer.lock
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]

## [0.9.0] - 2021-07-16
### Added
- First public release
106 changes: 106 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# PhpWeasyPrint

PhpWeasyPrint is a PHP library allowing PDF generation from an URL or an HTML page.
It's a wrapper for [WeasyPrint](https://weasyprint.org/), a smart solution helping web developers to create PDF documents, available everywhere Python runs.

You will have to download and install WeasyPrint to use PhpWeasyPrint (version 53 or greater is recommended).

This library is massively inspired by [KnpLabs/snappy](https://github.com/KnpLabs/snappy), of which it aims to be a one-to-one substitute (`GeneratorInterface` is the same).
See "[Differences with Snappy](#differences-with-snappy)" section to see how the two differs

## Installation using [Composer](http://getcomposer.org/)

```bash
$ composer require pontedilana/php-weasyprint
```

## Usage

### Initialization

```php
<?php

require __DIR__ . '/vendor/autoload.php';

use Pontedilana\PhpWeasyPrint\Pdf;

$pdf = new Pdf('/usr/local/bin/weasyprint');

// or you can do it in two steps
$pdf = new Pdf();
$pdf->setBinary('/usr/local/bin/weasyprint');
```

### Display the pdf in the browser

```php
$pdf = new Pdf('/usr/local/bin/weasyprint');
header('Content-Type: application/pdf');
echo $pdf->getOutput('http://www.github.com');
```

### Download the pdf from the browser

```php
$pdf = new Pdf('/usr/local/bin/weasyprint');
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="file.pdf"');
echo $pdf->getOutput('http://www.github.com');
```

### Generate local pdf file

```php
$pdf = new Pdf('/usr/local/bin/weasyprint');
$pdf->generateFromHtml('<h1>Bill</h1><p>You owe me money, dude.</p>', '/tmp/bill-123.pdf');
```

### Pass options to PhpWeasyPrint

```php
// Type weasyprint -h to see the list of options
$pdf = new Pdf('/usr/local/bin/weasyprint');
$pdf->setOption('encoding', 'utf8');
$pdf->setOption('media-type', 'screen');
$pdf->setOption('presentational-hints');
$pdf->setOption('optimize-size', 'all');
$pdf->setOption('stylesheet', ['/path/to/first-style.css', '/path/to/second-style.css']);
$pdf->setOption('attachment', ['/path/to/image.png', '/path/to/logo.jpg']);
```

### Reset options
Options can be reset to their initial values with `resetOptions()` method.

```php
$pdf = new Pdf('/usr/local/bin/weasyprint');
// Set some options
$pdf->setOption('media-type', 'screen');
// ..
// Reset options
$pdf->resetOptions();
```

## Differences with Snappy

Although PhpWeasyPrint and Snappy are interchangeable, there are a couple of differences between the two, due to WeasyPrint cli API:

* WeasyPrint doesn't support multiple sources to be merged in one single output pdf, so only one input source (string or URL) is accepted in PhpWeasyPrint;
* WeasyPrint version >= 53 doesn't generate images, so image generation from HTML tring or URL is possible only with WeasyPrint lower versions (`Pontedilana\PhpWeasyPrint\Image` has been successfully tested with Weasyprint 52.5).

## Bugs & Support

If you found a bug please fill a [detailed issue](https://github.com/pontedilana/php-weasyprint/issues) with all the following points.
If you need some help, please at least provide a complete reproducer so we could help you based on facts rather than assumptions.

* OS and its version
* WeasyPrint, its version and how you installed it
* A complete reproducer with relevant PHP and html/css/js code

If your reproducer is big, please try to shrink it. It will help everyone to narrow the bug.

## Credits

PhpWeasyPrint has been originally developed by the [Pontedilana](https://www.pontedilana.it) dev team.
Snappy has been originally developed by the [KnpLabs](http://knplabs.com) team.

40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "pontedilana/php-weasyprint",
"description": "PHP library allowing PDF generation from an url or a html page. Wrapper for Kozea/WeasyPrint.",
"type": "library",
"keywords": ["weasyprint", "pdf", "php"],
"require": {
"php": "^7.4||^8.0",
"psr/log": "^1.1",
"symfony/process": "^4.4||^5.3"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.0",
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^0.12",
"phpstan/phpstan-phpunit": "^0.12"
},
"license": "MIT",
"autoload": {
"psr-4": {
"Pontedilana\\PhpWeasyPrint\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\Unit\\Pontedilana\\PhpWeasyPrint\\": "tests/Unit/"
}
},
"authors": [
{
"name": "Pontedilana Dev Team",
"homepage": "https://www.pontedilana.it"
}
],
"scripts": {
"unit-tests": "vendor/bin/phpunit",
"static-analysis": "vendor/bin/phpstan analyse --ansi",
"check-cs": "vendor/bin/php-cs-fixer fix --diff --dry-run --verbose",
"fix-cs": "vendor/bin/php-cs-fixer fix --verbose"
}
}
19 changes: 19 additions & 0 deletions infection.json.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"source": {
"directories": [
"src"
]
},
"logs": {
"text": ".infection/infection.log",
"summary": ".infection/summary.log",
"json": ".infection/infection-log.json",
"perMutator": ".infection/per-mutator.md",
"github": true,
"badge": {
"branch": "main"
}
},
"minMsi": 84,
"minCoveredMsi": 89.7
}
11 changes: 11 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon

parameters:
level: 8
paths:
- src/
- tests/
inferPrivatePropertyTypeFromConstructor: true
reportUnmatchedIgnoredErrors: false
checkMissingIterableValueType: false
27 changes: 27 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
cacheResultFile=".phpunit.cache/test-results"
executionOrder="depends,defects"
resolveDependencies="true"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="false"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
failOnRisky="true"
failOnWarning="true"
verbose="true">
<testsuites>
<testsuite name="PhpWeasyPrint Unit Tests">
<directory>tests/Unit</directory>
</testsuite>
</testsuites>

<coverage cacheDirectory=".phpunit.cache/code-coverage"
processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
</coverage>
</phpunit>
Loading

0 comments on commit b0191db

Please sign in to comment.