{"id":16897748,"url":"https://github.com/endroid/pdf","last_synced_at":"2025-04-06T07:12:10.548Z","repository":{"id":56977762,"uuid":"117153733","full_name":"endroid/pdf","owner":"endroid","description":"Library for easy PDF generation using wkhtmltopdf and Snappy","archived":false,"fork":false,"pushed_at":"2024-10-20T18:39:27.000Z","size":64,"stargazers_count":35,"open_issues_count":0,"forks_count":5,"subscribers_count":4,"default_branch":"main","last_synced_at":"2025-03-30T06:05:35.986Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"https://endroid.nl/","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/endroid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"endroid"}},"created_at":"2018-01-11T21:08:15.000Z","updated_at":"2025-02-26T05:08:58.000Z","dependencies_parsed_at":"2024-10-27T12:13:30.373Z","dependency_job_id":null,"html_url":"https://github.com/endroid/pdf","commit_stats":{"total_commits":82,"total_committers":2,"mean_commits":41.0,"dds":0.04878048780487809,"last_synced_commit":"1c53d5b4f904be0289551a59e67922bba662c3ed"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/endroid%2Fpdf","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/endroid%2Fpdf/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/endroid%2Fpdf/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/endroid%2Fpdf/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/endroid","download_url":"https://codeload.github.com/endroid/pdf/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247445671,"owners_count":20939958,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-13T17:39:36.810Z","updated_at":"2025-04-06T07:12:10.525Z","avatar_url":"https://github.com/endroid.png","language":"PHP","funding_links":["https://github.com/sponsors/endroid"],"categories":[],"sub_categories":[],"readme":"# PDF\n\n*By [endroid](https://endroid.nl/)*\n\n[![Latest Stable Version](http://img.shields.io/packagist/v/endroid/pdf.svg)](https://packagist.org/packages/endroid/pdf)\n[![Build Status](https://github.com/endroid/pdf/workflows/CI/badge.svg)](https://github.com/endroid/pdf/actions)\n[![Total Downloads](http://img.shields.io/packagist/dt/endroid/pdf.svg)](https://packagist.org/packages/endroid/pdf)\n[![License](http://img.shields.io/packagist/l/endroid/pdf.svg)](https://packagist.org/packages/endroid/pdf)\n\nLibrary for easy PDF generation built around wkhtmltopdf and Snappy. Click\n[here](https://endroid.nl/play/pdf) for an example. Please note this example takes\nsome time to load because it contains a lot of pages and no caching is applied.\n\nRead the [blog](https://medium.com/@endroid/pdf-generation-in-symfony-3080702353b)\nfor more information on why I created this library and how to use it.\n\n## Easy data loading\n\nWhen you generate a PDF you need to make sure you pass the right contents to\nthe PDF object. This data can come from any source (a file, a URL, a controller)\nand some of these impose a performance hit so you often want to cache some of\nthese contents instead of loading the data every time you generate the PDF.\n\nThe [endroid/asset](https://github.com/endroid/asset) takes this burden away by\nallowing you to define your assets via a simple array of options. The asset\nfactory and guesser make sure the right type of asset is created and even\nprovide a so called cache asset that wraps any other asset.\n\n```php\n$this-\u003epdfBuilder\n    -\u003esetCover([\n        'controller' =\u003e CoverController::class,\n        'parameters' =\u003e ['title' =\u003e 'My PDF', 'date' =\u003e new DateTime()],\n        'cache_key' =\u003e 'cover',\n        'cache_expires_after' =\u003e 3600,\n        'cache_clear' =\u003e true, // use to purge any previously cached data\n    ])\n;\n```\n\nFor more information [read the documentation](https://github.com/endroid/asset).\n\n## Handling external resources\n\nAn HTML page can contain a number of external resources, each triggering a\nseparate request. However during PDF generation this can lead to performance or\neven stability issues. Therefor we need the number of requests to be as low as\npossible.\n\nThe [endroid/embed](https://github.com/endroid/embed) library helps you\nminimize the number of assets to load during PDF generation by allowing you to\nembed external resources via a Twig extension. You can use this extension to\nembed resources like fonts, stylesheets and scripts.\n\n```php\n\u003clink rel=\"stylesheet\" href=\"{{ embed(asset('/styles.css')) }}\"\u003e\n\n\u003cstyle\u003e\n@font-face {\n    font-family: 'SCP';\n    font-weight: normal;\n    src: url('{{ embed('https://fontlibrary.org/scp.ttf') }}');\n}\n\u003c/style\u003e\n```\n\nFor more information you can [read the documentation](https://github.com/endroid/embed).\n\n## The PDF builder\n\nWhen [endroid/installer](https://github.com/endroid/installer) detects Symfony\nthe builder is automatically wired and you can immediately start using it to\nbuild a PDF. This is an example of how you can use the builder.\n\n```php\n$pdfBuilder\n    -\u003esetCover([\n        'controller' =\u003e CoverController::class,\n        'cache_key' =\u003e 'cover',\n        'cache_expires_after' =\u003e 3600,\n    ])\n    -\u003esetTableOfContents([\n        'path' =\u003e '/var/www/html/table_of_contents.xml',\n        'cache_key' =\u003e 'toc',\n    ])\n    -\u003esetHeader([\n        'template' =\u003e 'pdf/header.html.twig',\n        'cache_key' =\u003e 'header',\n    ])\n    -\u003esetFooter([\n        'template' =\u003e 'pdf/footer.html.twig',\n        'cache_key' =\u003e 'footer',\n    ])\n    -\u003esetContent([\n        'url' =\u003e 'http://endroid.nl/',\n        'cache_key' =\u003e 'content',\n    ])\n    -\u003esetOptions([\n        'margin-top' =\u003e 16,\n        'margin-bottom' =\u003e 16,\n        'header-spacing' =\u003e 5,\n        'footer-spacing' =\u003e 5,\n    ])\n;\n\n$pdf = $pdfBuilder-\u003egetPdf();\n\n// Create a response object\n$response = InlinePdfResponse::createFromPdf($pdf);\n\n// Or output directly\nheader('Content-type: application/pdf');\necho $pdf-\u003egenerate();\n```\n\n## Installation\n\nUse [Composer](https://getcomposer.org/) to install the library.\n\n``` bash\n$ composer require endroid/pdf\n```\n\n### Symfony\n\nWhen you use Symfony, the [installer](https://github.com/endroid/installer)\nmakes sure that services are automatically wired. If the Snappy\\Pdf service is\nnot registered yet, make sure you create a service definition for it or install\nthe knplabs/snappy-bundle along with the library.\n\n``` bash\n$ composer require endroid/pdf knplabs/knp-snappy-bundle\n```\n\nAlso, if any of the asset types is unsupported (for instance because you have\nno cache component or Twig available) or if you simply don't want some to be\nregistered you can uncomment the adapter via the service configuration.\n\n### Bootstrapping the PDF builder\n\nWhen no autowiring is available you need to instantiate and wire the necessary\ndependencies yourself. You can do so via a bootstrap file for instance.\n\n```php\n$snappy = new Snappy(__DIR__.'/../vendor/h4cc/wkhtmltopdf-amd64/bin/wkhtmltopdf-amd64');\n\n$assetFactory = new AssetFactory();\n$assetFactory-\u003eadd(new DataAssetFactoryAdapter());\n$assetFactory-\u003eadd(new ControllerAssetFactoryAdapter($kernel, $requestStack));\n$assetFactory-\u003eadd(new TemplateAssetFactoryAdapter($twig));\n...\n\n$pdfBuilder = new PdfBuilder(new Pdf($snappy), $assetFactory);\n```\n\n## Versioning\n\nVersion numbers follow the MAJOR.MINOR.PATCH scheme. Backwards compatible\nchanges will be kept to a minimum but be aware that these can occur. Lock\nyour dependencies for production and test your code when upgrading.\n\n## License\n\nThis bundle is under the MIT license. For the full copyright and license\ninformation please view the LICENSE file that was distributed with this source code.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fendroid%2Fpdf","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fendroid%2Fpdf","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fendroid%2Fpdf/lists"}