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\ObjectNormalizer
;
18 private $entries = array();
19 private $authors = array('wallabag');
20 private $language = '';
21 private $tags = array();
22 private $footerTemplate = '<div style="text-align:center;">
23 <p>Produced by wallabag with %EXPORT_METHOD%</p>
24 <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>
28 * @param string $wallabagUrl Wallabag instance url
29 * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE
31 public function __construct($wallabagUrl, $logoPath)
33 $this->wallabagUrl
= $wallabagUrl;
34 $this->logoPath
= $logoPath;
40 * @param array|Entry $entries An array of entries or one entry
42 public function setEntries($entries)
44 if (!is_array($entries)) {
45 $this->language
= $entries->getLanguage();
46 $entries = array($entries);
49 $this->entries
= $entries;
51 foreach ($entries as $entry) {
52 $this->tags
[] = $entry->getTags();
59 * Sets the category of which we want to get articles, or just one entry.
61 * @param string $method Method to get articles
63 public function updateTitle($method)
65 $this->title
= $method.' articles';
67 if ('entry' === $method) {
68 $this->title
= $this->entries
[0]->getTitle();
75 * Sets the output format.
77 * @param string $format
79 public function exportAs($format)
83 return $this->produceEpub();
86 return $this->produceMobi();
89 return $this->producePDF();
92 return $this->produceCSV();
95 return $this->produceJSON();
98 return $this->produceXML();
101 throw new \
InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
105 * Use PHPePub to dump a .epub file.
107 private function produceEpub()
110 * Start and End of the book
113 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
114 ."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
116 ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
117 ."<title>wallabag articles book</title>\n"
121 $bookEnd = "</body>\n</html>\n";
123 $book = new EPub(EPub
::BOOK_VERSION_EPUB3
);
129 $book->setTitle($this->title
);
130 // Could also be the ISBN number, prefered for published books, or a UUID.
131 $book->setIdentifier($this->title
, EPub
::IDENTIFIER_URI
);
132 // 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.
133 $book->setLanguage($this->language
);
134 $book->setDescription('Some articles saved on my wallabag');
136 foreach ($this->authors
as $author) {
137 $book->setAuthor($author, $author);
140 // I hope this is a non existant address :)
141 $book->setPublisher('wallabag', 'wallabag');
142 // Strictly not needed as the book date defaults to time().
143 $book->setDate(time());
144 $book->setSourceURL($this->wallabagUrl
);
146 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'PHP');
147 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'wallabag');
152 if (file_exists($this->logoPath
)) {
153 $book->setCoverImage('Cover.png', file_get_contents($this->logoPath
), 'image/png');
156 $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd);
161 * Adding actual entries
164 // set tags as subjects
165 foreach ($this->entries
as $entry) {
166 foreach ($this->tags
as $tag) {
167 $book->setSubject($tag['value']);
170 $chapter = $content_start.$entry->getContent().$bookEnd;
171 $book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub
::EXTERNAL_REF_ADD
);
174 return Response
::create(
178 'Content-Description' => 'File Transfer',
179 'Content-type' => 'application/epub+zip',
180 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.epub"',
181 'Content-Transfer-Encoding' => 'binary',
187 * Use PHPMobi to dump a .mobi file.
189 private function produceMobi()
192 $content = new \
MOBIFile();
197 $content->set('title', $this->title
);
198 $content->set('author', implode($this->authors
));
199 $content->set('subject', $this->title
);
204 $content->appendParagraph($this->getExportInformation('PHPMobi'));
205 if (file_exists($this->logoPath
)) {
206 $content->appendImage(imagecreatefrompng($this->logoPath
));
208 $content->appendPageBreak();
211 * Adding actual entries
213 foreach ($this->entries
as $entry) {
214 $content->appendChapterTitle($entry->getTitle());
215 $content->appendParagraph($entry->getContent());
216 $content->appendPageBreak();
218 $mobi->setContentProvider($content);
220 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
221 $this->title
= preg_replace('/[^A-Za-z0-9\-]/', '', $this->title
);
223 return Response
::create(
227 'Accept-Ranges' => 'bytes',
228 'Content-Description' => 'File Transfer',
229 'Content-type' => 'application/x-mobipocket-ebook',
230 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.mobi"',
231 'Content-Transfer-Encoding' => 'binary',
237 * Use TCPDF to dump a .pdf file.
239 private function producePDF()
241 $pdf = new \
TCPDF(PDF_PAGE_ORIENTATION
, PDF_UNIT
, PDF_PAGE_FORMAT
, true, 'UTF-8', false);
246 $pdf->SetCreator(PDF_CREATOR
);
247 $pdf->SetAuthor('wallabag');
248 $pdf->SetTitle($this->title
);
249 $pdf->SetSubject('Articles via wallabag');
250 $pdf->SetKeywords('wallabag');
256 $intro = '<h1>'.$this->title
.'</h1>'.$this->getExportInformation('tcpdf');
258 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
261 * Adding actual entries
263 foreach ($this->entries
as $entry) {
264 foreach ($this->tags
as $tag) {
265 $pdf->SetKeywords($tag['value']);
269 $html = '<h1>'.$entry->getTitle().'</h1>';
270 $html .= $entry->getContent();
272 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
275 // set image scale factor
276 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO
);
278 return Response
::create(
279 $pdf->Output('', 'S'),
282 'Content-Description' => 'File Transfer',
283 'Content-type' => 'application/pdf',
284 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.pdf"',
285 'Content-Transfer-Encoding' => 'binary',
291 * Inspired from CsvFileDumper.
293 private function produceCSV()
297 $handle = fopen('php://memory', 'rb+');
299 fputcsv($handle, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'), $delimiter, $enclosure);
301 foreach ($this->entries
as $entry) {
307 $entry->getContent(),
308 implode(', ', $entry->getTags()->toArray()),
309 $entry->getMimetype(),
310 $entry->getLanguage(),
318 $output = stream_get_contents($handle);
321 return Response
::create(
325 'Content-type' => 'application/csv',
326 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.csv"',
327 'Content-Transfer-Encoding' => 'UTF-8',
332 private function produceJSON()
334 $serializer = $this->prepareSerializingContent();
335 $jsonContent = $serializer->serialize($this->entries
, 'json');
337 return Response
::create(
341 'Content-type' => 'application/json',
342 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.json"',
343 'Content-Transfer-Encoding' => 'UTF-8',
348 private function produceXML()
350 $serializer = $this->prepareSerializingContent();
351 $xmlContent = $serializer->serialize($this->entries
, 'xml');
353 return Response
::create(
357 'Content-type' => 'application/xml',
358 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.xml"',
359 'Content-Transfer-Encoding' => 'UTF-8',
364 * Return a Serializer object for producing processes that need it (JSON & XML).
368 private function prepareSerializingContent()
370 $encoders = array(new XmlEncoder(), new JsonEncoder());
371 $normalizers = array(new ObjectNormalizer());
372 $normalizers[0]->setIgnoredAttributes(array('user', 'createdAt', 'updatedAt'));
374 return new Serializer($normalizers, $encoders);
378 * Return a kind of footer / information for the epub.
380 * @param string $type Generator of the export, can be: tdpdf, PHPePub, PHPMobi
384 private function getExportInformation($type)
386 $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate
);
388 if ('tcpdf' === $type) {
389 return str_replace('%IMAGE%', '<img src="'.$this->logoPath
.'" />', $info);
392 return str_replace('%IMAGE%', '', $info);