3 namespace Wallabag\CoreBundle\Helper
;
6 use JMS\Serializer\SerializationContext
;
7 use JMS\Serializer\SerializerBuilder
;
9 use PHPePub\Core\Structure\OPF\DublinCore
;
10 use Symfony\Component\HttpFoundation\Response
;
11 use Craue\ConfigBundle\Util\Config
;
14 * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest.
21 private $entries = [];
22 private $authors = ['wallabag'];
23 private $language = '';
24 private $footerTemplate = '<div style="text-align:center;">
25 <p>Produced by wallabag with %EXPORT_METHOD%</p>
26 <p>Please open <a href="https://github.com/wallabag/wallabag/issues">an issue</a> if you have trouble with the display of this E-Book on your device.</p>
30 * @param Config $craueConfig CraueConfig instance to get wallabag instance url from database
31 * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE
33 public function __construct(Config
$craueConfig, $logoPath)
35 $this->wallabagUrl
= $craueConfig->get('wallabag_url');
36 $this->logoPath
= $logoPath;
42 * @param array|Entry $entries An array of entries or one entry
44 * @return EntriesExport
46 public function setEntries($entries)
48 if (!is_array($entries)) {
49 $this->language
= $entries->getLanguage();
50 $entries = [$entries];
53 $this->entries
= $entries;
59 * Sets the category of which we want to get articles, or just one entry.
61 * @param string $method Method to get articles
63 * @return EntriesExport
65 public function updateTitle($method)
67 $this->title
= $method.' articles';
69 if ('entry' === $method) {
70 $this->title
= $this->entries
[0]->getTitle();
77 * Sets the output format.
79 * @param string $format
83 public function exportAs($format)
85 $functionName = 'produce'.ucfirst($format);
86 if (method_exists($this, $functionName)) {
87 return $this->$functionName();
90 throw new \
InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
94 * Use PHPePub to dump a .epub file.
98 private function produceEpub()
101 * Start and End of the book
104 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
105 ."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
107 ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
108 ."<title>wallabag articles book</title>\n"
112 $bookEnd = "</body>\n</html>\n";
114 $book = new EPub(EPub
::BOOK_VERSION_EPUB3
);
120 $book->setTitle($this->title
);
121 // Could also be the ISBN number, prefered for published books, or a UUID.
122 $book->setIdentifier($this->title
, EPub
::IDENTIFIER_URI
);
123 // Not needed, but included for the example, Language is mandatory, but EPub defaults to "en". Use RFC3066 Language codes, such as "en", "da", "fr" etc.
124 $book->setLanguage($this->language
);
125 $book->setDescription('Some articles saved on my wallabag');
127 foreach ($this->authors
as $author) {
128 $book->setAuthor($author, $author);
131 // I hope this is a non existant address :)
132 $book->setPublisher('wallabag', 'wallabag');
133 // Strictly not needed as the book date defaults to time().
134 $book->setDate(time());
135 $book->setSourceURL($this->wallabagUrl
);
137 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'PHP');
138 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'wallabag');
143 if (file_exists($this->logoPath
)) {
144 $book->setCoverImage('Cover.png', file_get_contents($this->logoPath
), 'image/png');
147 $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd);
152 * Adding actual entries
155 // set tags as subjects
156 foreach ($this->entries
as $entry) {
157 foreach ($entry->getTags() as $tag) {
158 $book->setSubject($tag->getLabel());
161 // the reader in Kobo Devices doesn't likes special caracters
162 // in filenames, we limit to A-z/0-9
163 $filename = preg_replace('/[^A-Za-z0-9\-]/', '', $entry->getTitle());
165 $chapter = $content_start.$entry->getContent().$bookEnd;
166 $book->addChapter($entry->getTitle(), htmlspecialchars($filename).'.html', $chapter, true, EPub
::EXTERNAL_REF_ADD
);
169 return Response
::create(
173 'Content-Description' => 'File Transfer',
174 'Content-type' => 'application/epub+zip',
175 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.epub"',
176 'Content-Transfer-Encoding' => 'binary',
182 * Use PHPMobi to dump a .mobi file.
186 private function produceMobi()
189 $content = new \
MOBIFile();
194 $content->set('title', $this->title
);
195 $content->set('author', implode($this->authors
));
196 $content->set('subject', $this->title
);
201 $content->appendParagraph($this->getExportInformation('PHPMobi'));
202 if (file_exists($this->logoPath
)) {
203 $content->appendImage(imagecreatefrompng($this->logoPath
));
205 $content->appendPageBreak();
208 * Adding actual entries
210 foreach ($this->entries
as $entry) {
211 $content->appendChapterTitle($entry->getTitle());
212 $content->appendParagraph($entry->getContent());
213 $content->appendPageBreak();
215 $mobi->setContentProvider($content);
217 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
218 $this->title
= preg_replace('/[^A-Za-z0-9\-]/', '', $this->title
);
220 return Response
::create(
224 'Accept-Ranges' => 'bytes',
225 'Content-Description' => 'File Transfer',
226 'Content-type' => 'application/x-mobipocket-ebook',
227 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.mobi"',
228 'Content-Transfer-Encoding' => 'binary',
234 * Use TCPDF to dump a .pdf file.
238 private function producePdf()
240 $pdf = new \
TCPDF(PDF_PAGE_ORIENTATION
, PDF_UNIT
, PDF_PAGE_FORMAT
, true, 'UTF-8', false);
245 $pdf->SetCreator(PDF_CREATOR
);
246 $pdf->SetAuthor('wallabag');
247 $pdf->SetTitle($this->title
);
248 $pdf->SetSubject('Articles via wallabag');
249 $pdf->SetKeywords('wallabag');
255 $intro = '<h1>'.$this->title
.'</h1>'.$this->getExportInformation('tcpdf');
257 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
260 * Adding actual entries
262 foreach ($this->entries
as $entry) {
263 foreach ($entry->getTags() as $tag) {
264 $pdf->SetKeywords($tag->getLabel());
268 $html = '<h1>'.$entry->getTitle().'</h1>';
269 $html .= $entry->getContent();
271 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
274 // set image scale factor
275 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO
);
277 return Response
::create(
278 $pdf->Output('', 'S'),
281 'Content-Description' => 'File Transfer',
282 'Content-type' => 'application/pdf',
283 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.pdf"',
284 'Content-Transfer-Encoding' => 'binary',
290 * Inspired from CsvFileDumper.
294 private function produceCsv()
298 $handle = fopen('php://memory', 'rb+');
300 fputcsv($handle, ['Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language', 'Creation date'], $delimiter, $enclosure);
302 foreach ($this->entries
as $entry) {
308 // remove new line to avoid crazy results
309 str_replace(["\r\n", "\r", "\n"], '', $entry->getContent()),
310 implode(', ', $entry->getTags()->toArray()),
311 $entry->getMimetype(),
312 $entry->getLanguage(),
313 $entry->getCreatedAt()->format('d/m/Y h:i:s'),
321 $output = stream_get_contents($handle);
324 return Response
::create(
328 'Content-type' => 'application/csv',
329 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.csv"',
330 'Content-Transfer-Encoding' => 'UTF-8',
340 private function produceJson()
342 return Response
::create(
343 $this->prepareSerializingContent('json'),
346 'Content-type' => 'application/json',
347 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.json"',
348 'Content-Transfer-Encoding' => 'UTF-8',
358 private function produceXml()
360 return Response
::create(
361 $this->prepareSerializingContent('xml'),
364 'Content-type' => 'application/xml',
365 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.xml"',
366 'Content-Transfer-Encoding' => 'UTF-8',
376 private function produceTxt()
379 $bar = str_repeat('=', 100);
380 foreach ($this->entries
as $entry) {
381 $content .= "\n\n".$bar."\n\n".$entry->getTitle()."\n\n".$bar."\n\n";
382 $content .= trim(preg_replace('/\s+/S', ' ', strip_tags($entry->getContent())))."\n\n";
385 return Response
::create(
389 'Content-type' => 'text/plain',
390 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.txt"',
391 'Content-Transfer-Encoding' => 'UTF-8',
397 * Return a Serializer object for producing processes that need it (JSON & XML).
399 * @param string $format
403 private function prepareSerializingContent($format)
405 $serializer = SerializerBuilder
::create()->build();
407 return $serializer->serialize(
410 SerializationContext
::create()->setGroups(['entries_for_user'])
415 * Return a kind of footer / information for the epub.
417 * @param string $type Generator of the export, can be: tdpdf, PHPePub, PHPMobi
421 private function getExportInformation($type)
423 $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate
);
425 if ('tcpdf' === $type) {
426 return str_replace('%IMAGE%', '<img src="'.$this->logoPath
.'" />', $info);
429 return str_replace('%IMAGE%', '', $info);