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
;
13 * This class doesn't have unit test BUT it's fully covered by a functional test with ExportControllerTest.
20 private $entries = array();
21 private $authors = array('wallabag');
22 private $language = '';
23 private $tags = array();
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 string $wallabagUrl Wallabag instance url
31 * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE
33 public function __construct($wallabagUrl, $logoPath)
35 $this->wallabagUrl
= $wallabagUrl;
36 $this->logoPath
= $logoPath;
42 * @param array|Entry $entries An array of entries or one entry
44 public function setEntries($entries)
46 if (!is_array($entries)) {
47 $this->language
= $entries->getLanguage();
48 $entries = array($entries);
51 $this->entries
= $entries;
53 foreach ($entries as $entry) {
54 $this->tags
[] = $entry->getTags();
61 * Sets the category of which we want to get articles, or just one entry.
63 * @param string $method Method to get articles
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
81 public function exportAs($format)
85 return $this->produceEpub();
88 return $this->produceMobi();
91 return $this->producePDF();
94 return $this->produceCSV();
97 return $this->produceJSON();
100 return $this->produceXML();
103 throw new \
InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
107 * Use PHPePub to dump a .epub file.
109 private function produceEpub()
112 * Start and End of the book
115 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
116 ."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
118 ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
119 ."<title>wallabag articles book</title>\n"
123 $bookEnd = "</body>\n</html>\n";
125 $book = new EPub(EPub
::BOOK_VERSION_EPUB3
);
131 $book->setTitle($this->title
);
132 // Could also be the ISBN number, prefered for published books, or a UUID.
133 $book->setIdentifier($this->title
, EPub
::IDENTIFIER_URI
);
134 // 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.
135 $book->setLanguage($this->language
);
136 $book->setDescription('Some articles saved on my wallabag');
138 foreach ($this->authors
as $author) {
139 $book->setAuthor($author, $author);
142 // I hope this is a non existant address :)
143 $book->setPublisher('wallabag', 'wallabag');
144 // Strictly not needed as the book date defaults to time().
145 $book->setDate(time());
146 $book->setSourceURL($this->wallabagUrl
);
148 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'PHP');
149 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'wallabag');
154 if (file_exists($this->logoPath
)) {
155 $book->setCoverImage('Cover.png', file_get_contents($this->logoPath
), 'image/png');
158 $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd);
163 * Adding actual entries
166 // set tags as subjects
167 foreach ($this->entries
as $entry) {
168 foreach ($this->tags
as $tag) {
169 $book->setSubject($tag['value']);
172 $chapter = $content_start.$entry->getContent().$bookEnd;
173 $book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub
::EXTERNAL_REF_ADD
);
176 return Response
::create(
180 'Content-Description' => 'File Transfer',
181 'Content-type' => 'application/epub+zip',
182 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.epub"',
183 'Content-Transfer-Encoding' => 'binary',
189 * Use PHPMobi to dump a .mobi file.
191 private function produceMobi()
194 $content = new \
MOBIFile();
199 $content->set('title', $this->title
);
200 $content->set('author', implode($this->authors
));
201 $content->set('subject', $this->title
);
206 $content->appendParagraph($this->getExportInformation('PHPMobi'));
207 if (file_exists($this->logoPath
)) {
208 $content->appendImage(imagecreatefrompng($this->logoPath
));
210 $content->appendPageBreak();
213 * Adding actual entries
215 foreach ($this->entries
as $entry) {
216 $content->appendChapterTitle($entry->getTitle());
217 $content->appendParagraph($entry->getContent());
218 $content->appendPageBreak();
220 $mobi->setContentProvider($content);
222 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
223 $this->title
= preg_replace('/[^A-Za-z0-9\-]/', '', $this->title
);
225 return Response
::create(
229 'Accept-Ranges' => 'bytes',
230 'Content-Description' => 'File Transfer',
231 'Content-type' => 'application/x-mobipocket-ebook',
232 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.mobi"',
233 'Content-Transfer-Encoding' => 'binary',
239 * Use TCPDF to dump a .pdf file.
241 private function producePDF()
243 $pdf = new \
TCPDF(PDF_PAGE_ORIENTATION
, PDF_UNIT
, PDF_PAGE_FORMAT
, true, 'UTF-8', false);
248 $pdf->SetCreator(PDF_CREATOR
);
249 $pdf->SetAuthor('wallabag');
250 $pdf->SetTitle($this->title
);
251 $pdf->SetSubject('Articles via wallabag');
252 $pdf->SetKeywords('wallabag');
258 $intro = '<h1>'.$this->title
.'</h1>'.$this->getExportInformation('tcpdf');
260 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
263 * Adding actual entries
265 foreach ($this->entries
as $entry) {
266 foreach ($this->tags
as $tag) {
267 $pdf->SetKeywords($tag['value']);
271 $html = '<h1>'.$entry->getTitle().'</h1>';
272 $html .= $entry->getContent();
274 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
277 // set image scale factor
278 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO
);
280 return Response
::create(
281 $pdf->Output('', 'S'),
284 'Content-Description' => 'File Transfer',
285 'Content-type' => 'application/pdf',
286 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.pdf"',
287 'Content-Transfer-Encoding' => 'binary',
293 * Inspired from CsvFileDumper.
295 private function produceCSV()
299 $handle = fopen('php://memory', 'rb+');
301 fputcsv($handle, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'), $delimiter, $enclosure);
303 foreach ($this->entries
as $entry) {
309 // remove new line to avoid crazy results
310 str_replace(array("\r\n", "\r", "\n"), '', $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 $serializer = SerializerBuilder
::create()->build();
370 return $serializer->serialize(
373 SerializationContext
::create()->setGroups(array('entries_for_user'))
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);