Installation:
composer require anglemx/pdf-viewer-bundle
Enable the bundle in config/bundles.php:
Angle\PDFViewerBundle\AnglePDFViewerBundle::class => ['all' => true],
Configure Routing:
Add to config/routes.yaml:
angle_pdf_viewer:
resource: "@AnglePDFViewerBundle/Resources/config/routes.yaml"
First Use Case: Redirect to the viewer with a PDF path:
return $this->redirectToRoute('angle_pdf_viewer_view', ['file' => '/path/to/your/file.pdf']);
Or embed in Twig:
<a href="{{ path('angle_pdf_viewer_view', {'file': '/path/to/file.pdf'}) }}">View PDF</a>
Quick Test:
Access /_pdf?file=/bundles/anglepdfviewer/vendor/pdfjs/web/compressed.tracemonkey-pldi-09.pdf to verify the bundle works.
Embedding PDFs in Views: Use Twig to generate links or embed the viewer directly:
{% embed('angle_pdf_viewer::viewer.html.twig', {
file: '/path/to/document.pdf',
width: 800,
height: 600
}) %}
{% endembed %}
Dynamic PDF Paths: Pass dynamic paths via controllers or services:
$pdfPath = $this->getPdfService()->getPath($userId);
return $this->render('pdf_viewer.html.twig', ['pdf_path' => $pdfPath]);
Customizing Viewer Options:
Extend the Twig template (viewer.html.twig) to override default PDF.js settings:
{{ parent() }}
<script>
PDFViewerApplication.options = {
...PDFViewerApplication.options,
defaultZoomValue: 1.5,
disableAutoFetch: true
};
</script>
Integration with Forms: Use the viewer for previewing uploaded PDFs:
<form method="post" enctype="multipart/form-data">
<input type="file" name="pdf_file">
{% if uploadedPdf %}
<a href="{{ path('angle_pdf_viewer_view', {'file': uploadedPdf.path}) }}">Preview</a>
{% endif %}
</form>
API-Driven PDF Delivery: Serve PDFs via API endpoints and pass paths dynamically:
// Controller
public function showPdf(UploadedFile $file): Response
{
$tempPath = $file->getRealPath();
return $this->redirectToRoute('angle_pdf_viewer_view', ['file' => $tempPath]);
}
File Path Security:
UrlGenerator or Asset component for trusted paths:
$safePath = $this->getParameter('kernel.project_dir').'/uploads/'.$userId.'/file.pdf';
CORS and External PDFs:
PDF.js Version Lock:
v4.0.379, which may lack critical updates.<script src="https://cdnjs.cloudflare.com/ajax/libs/pdf.js/4.0.379/pdf.min.js"></script>
Twig Template Overrides:
{% extends %} in custom templates to inherit parent functionality:
{% extends '@AnglePDFViewer/viewer.html.twig' %}
{% block pdfjs_options %}
{{ parent() }}
<script>PDFViewerApplication.options.collab = { enabled: true };</script>
{% endblock %}
Large PDF Performance:
<script>
PDFViewerApplication.options.workerSrc = '/path/to/pdf.worker.min.js';
PDFViewerApplication.options.maxCanvasPixels = 1000 * 1000; // Limit canvas size
</script>
Check Browser Console:
pdf.js often appear in the console. Look for PDF.js vX.X.X logs.Verify File Access:
curl -I http://localhost/path/to/file.pdf
Disable Cache:
<script src="{{ asset('bundles/anglepdfviewer/pdf.js?v='~now()) }}"></script>
Log Routes:
php bin/console debug:router | grep angle_pdf_viewer
Custom PDF.js Build:
config/packages/angle_pdf_viewer.yaml:
angle_pdf_viewer:
pdfjs_path: '/custom/path/to/pdf.js'
Add Annotations:
<script src="{{ asset('bundles/anglepdfviewer/pdfjs-plugin-annotations.js') }}"></script>
<script>
PDFViewerApplication.options.plugins = [new PDFAnnotations()];
</script>
Authentication:
# config/routes.yaml
angle_pdf_viewer:
resource: "@AnglePDFViewerBundle/Resources/config/routes.yaml"
options:
expose: true
// src/Security/Voter/PdfViewerVoter.php
public function supportsAttribute($attribute): bool
{
return $attribute === 'VIEW_PDF';
}
Event Listeners:
// src/EventListener/PdfViewerListener.php
public function onPdfView(PdfViewEvent $event)
{
if (!$this->isValidPath($event->getPath())) {
throw new \RuntimeException('Invalid PDF path');
}
}
How can I help you explore Laravel packages today?