3 namespace Wallabag\CoreBundle\Helper
;
6 use PHPePub\Core\Structure\OPF\DublinCore
;
7 use Symfony\Component\HttpFoundation\Response
;
8 use Symfony\Component\Serializer\Serializer
;
9 use Symfony\Component\Serializer\Encoder\XmlEncoder
;
10 use Symfony\Component\Serializer\Encoder\JsonEncoder
;
11 use Symfony\Component\Serializer\Normalizer\PropertyNormalizer
;
12 use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader
;
13 use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory
;
14 use Doctrine\Common\Annotations\AnnotationReader
;
21 private $entries = array();
22 private $authors = array('wallabag');
23 private $language = '';
24 private $tags = array();
25 private $footerTemplate = '<div style="text-align:center;">
26 <p>Produced by wallabag with %EXPORT_METHOD%</p>
27 <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>
31 * @param string $wallabagUrl Wallabag instance url
32 * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE
34 public function __construct($wallabagUrl, $logoPath)
36 $this->wallabagUrl
= $wallabagUrl;
37 $this->logoPath
= $logoPath;
43 * @param array|Entry $entries An array of entries or one entry
45 public function setEntries($entries)
47 if (!is_array($entries)) {
48 $this->language
= $entries->getLanguage();
49 $entries = array($entries);
52 $this->entries
= $entries;
54 foreach ($entries as $entry) {
55 $this->tags
[] = $entry->getTags();
62 * Sets the category of which we want to get articles, or just one entry.
64 * @param string $method Method to get articles
66 public function updateTitle($method)
68 $this->title
= $method.' articles';
70 if ('entry' === $method) {
71 $this->title
= $this->entries
[0]->getTitle();
78 * Sets the output format.
80 * @param string $format
82 public function exportAs($format)
86 return $this->produceEpub();
89 return $this->produceMobi();
92 return $this->producePDF();
95 return $this->produceCSV();
98 return $this->produceJSON();
101 return $this->produceXML();
104 throw new \
InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
108 * Use PHPePub to dump a .epub file.
110 private function produceEpub()
113 * Start and End of the book
116 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
117 ."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
119 ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
120 ."<title>wallabag articles book</title>\n"
124 $bookEnd = "</body>\n</html>\n";
126 $book = new EPub(EPub
::BOOK_VERSION_EPUB3
);
132 $book->setTitle($this->title
);
133 // Could also be the ISBN number, prefered for published books, or a UUID.
134 $book->setIdentifier($this->title
, EPub
::IDENTIFIER_URI
);
135 // 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.
136 $book->setLanguage($this->language
);
137 $book->setDescription('Some articles saved on my wallabag');
139 foreach ($this->authors
as $author) {
140 $book->setAuthor($author, $author);
143 // I hope this is a non existant address :)
144 $book->setPublisher('wallabag', 'wallabag');
145 // Strictly not needed as the book date defaults to time().
146 $book->setDate(time());
147 $book->setSourceURL($this->wallabagUrl
);
149 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'PHP');
150 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'wallabag');
155 if (file_exists($this->logoPath
)) {
156 $book->setCoverImage('Cover.png', file_get_contents($this->logoPath
), 'image/png');
159 $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd);
164 * Adding actual entries
167 // set tags as subjects
168 foreach ($this->entries
as $entry) {
169 foreach ($this->tags
as $tag) {
170 $book->setSubject($tag['value']);
173 $chapter = $content_start.$entry->getContent().$bookEnd;
174 $book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub
::EXTERNAL_REF_ADD
);
177 return Response
::create(
181 'Content-Description' => 'File Transfer',
182 'Content-type' => 'application/epub+zip',
183 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.epub"',
184 'Content-Transfer-Encoding' => 'binary',
190 * Use PHPMobi to dump a .mobi file.
192 private function produceMobi()
195 $content = new \
MOBIFile();
200 $content->set('title', $this->title
);
201 $content->set('author', implode($this->authors
));
202 $content->set('subject', $this->title
);
207 $content->appendParagraph($this->getExportInformation('PHPMobi'));
208 if (file_exists($this->logoPath
)) {
209 $content->appendImage(imagecreatefrompng($this->logoPath
));
211 $content->appendPageBreak();
214 * Adding actual entries
216 foreach ($this->entries
as $entry) {
217 $content->appendChapterTitle($entry->getTitle());
218 $content->appendParagraph($entry->getContent());
219 $content->appendPageBreak();
221 $mobi->setContentProvider($content);
223 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
224 $this->title
= preg_replace('/[^A-Za-z0-9\-]/', '', $this->title
);
226 return Response
::create(
230 'Accept-Ranges' => 'bytes',
231 'Content-Description' => 'File Transfer',
232 'Content-type' => 'application/x-mobipocket-ebook',
233 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.mobi"',
234 'Content-Transfer-Encoding' => 'binary',
240 * Use TCPDF to dump a .pdf file.
242 private function producePDF()
244 $pdf = new \
TCPDF(PDF_PAGE_ORIENTATION
, PDF_UNIT
, PDF_PAGE_FORMAT
, true, 'UTF-8', false);
249 $pdf->SetCreator(PDF_CREATOR
);
250 $pdf->SetAuthor('wallabag');
251 $pdf->SetTitle($this->title
);
252 $pdf->SetSubject('Articles via wallabag');
253 $pdf->SetKeywords('wallabag');
259 $intro = '<h1>'.$this->title
.'</h1>'.$this->getExportInformation('tcpdf');
261 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
264 * Adding actual entries
266 foreach ($this->entries
as $entry) {
267 foreach ($this->tags
as $tag) {
268 $pdf->SetKeywords($tag['value']);
272 $html = '<h1>'.$entry->getTitle().'</h1>';
273 $html .= $entry->getContent();
275 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
278 // set image scale factor
279 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO
);
281 return Response
::create(
282 $pdf->Output('', 'S'),
285 'Content-Description' => 'File Transfer',
286 'Content-type' => 'application/pdf',
287 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.pdf"',
288 'Content-Transfer-Encoding' => 'binary',
294 * Inspired from CsvFileDumper.
296 private function produceCSV()
300 $handle = fopen('php://memory', 'rb+');
302 fputcsv($handle, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'), $delimiter, $enclosure);
304 foreach ($this->entries
as $entry) {
310 $entry->getContent(),
311 implode(', ', $entry->getTags()->toArray()),
312 $entry->getMimetype(),
313 $entry->getLanguage(),
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',
335 private function produceJSON()
337 return Response
::create(
338 $this->prepareSerializingContent('json'),
341 'Content-type' => 'application/json',
342 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.json"',
343 'Content-Transfer-Encoding' => 'UTF-8',
348 private function produceXML()
350 return Response
::create(
351 $this->prepareSerializingContent('xml'),
354 'Content-type' => 'application/xml',
355 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.xml"',
356 'Content-Transfer-Encoding' => 'UTF-8',
362 * Return a Serializer object for producing processes that need it (JSON & XML).
366 private function prepareSerializingContent($format)
368 $encoders = array(new XmlEncoder(), new JsonEncoder());
369 $classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
370 $normalizers = array(new PropertyNormalizer($classMetadataFactory));
371 $serializer = new Serializer($normalizers, $encoders);
373 return $serializer->serialize($this->entries
, $format, array('groups' => array('entries_for_user')));
377 * Return a kind of footer / information for the epub.
379 * @param string $type Generator of the export, can be: tdpdf, PHPePub, PHPMobi
383 private function getExportInformation($type)
385 $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate
);
387 if ('tcpdf' === $type) {
388 return str_replace('%IMAGE%', '<img src="'.$this->logoPath
.'" />', $info);
391 return str_replace('%IMAGE%', '', $info);