You may have the possibility to convert Markdown files into PDF. You just need to wrap your markdown file into an HTML or Twig file.
Warning
Every HTML or Twig template you pass to Gotenberg need to have the following structure.
Even Header or Footer parts.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>My PDF</title>
</head>
<body>
<!-- Your code goes here -->
</body>
</html>
The HTML file to wrap markdown file into PDF.
Warning
As assets files, by default the HTML files are fetch in the assets folder of
your application.
For more information about path resolution go to assets documentation.
Warning
In the template, you must use the {{ toHTML "filename.md" }} special directive to reference the Markdown file. The HTML template that receives your markdown file will look like this.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My PDF</title>
</head>
<body>
{{ toHTML "content.md" }}
</body>
</html>
namespace App\Controller;
use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;
class YourController
{
public function yourControllerMethod(GotenbergPdfInterface $gotenberg): Response
{
return $gotenberg->markdown()
->wrapperFile('../templates/wrapper.html')
->files('content.md')
->generate()
;
}
}
The Twig file to convert into PDF.
Warning
In the template, you must use the {{ toHTML "filename.md" }} special directive to reference the Markdown file. The twig template that receives your markdown file will look like this.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>My PDF</title>
</head>
<body>
{% verbatim %}
{{ toHTML "content.md" }}
{% endverbatim %}
</body>
</html>
Gotenberg expects an HTML template containing the directive {{ toHTML "filename.md" }}. To prevent any conflict, you may want to use the verbatim tag to encapsulate the directive.
namespace App\Controller;
use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;
class YourController
{
public function yourControllerMethod(GotenbergPdfInterface $gotenberg): Response
{
return $gotenberg->markdown()
->wrapper('wrapper.html.twig', [
'my_var' => 'value'
])
->files('content.md')
->generate()
;
}
}
Required to generate a PDF from Markdown builder. You can pass several files with that method.
namespace App\Controller;
use Sensiolabs\GotenbergBundle\GotenbergPdfInterface;
class YourController
{
public function yourControllerMethod(GotenbergPdfInterface $gotenberg): Response
{
return $gotenberg->markdown()
->wrapper('wrapper.html.twig', [
'my_var' => 'value'
])
->files(
'header.md',
'content.md',
'footer.md',
)
->generate()
;
}
}
Tip
For more information go to PDF customization.