3 namespace Wallabag\CoreBundle\Helper
;
6 use PHPePub\Core\Structure\OPF\DublinCore
;
7 use Symfony\Component\HttpFoundation\Response
;
9 use JMS\Serializer\SerializerBuilder
;
10 use JMS\Serializer\SerializationContext
;
17 private $entries = array();
18 private $authors = array('wallabag');
19 private $language = '';
20 private $tags = array();
21 private $footerTemplate = '<div style="text-align:center;">
22 <p>Produced by wallabag with %EXPORT_METHOD%</p>
23 <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>
27 * @param string $wallabagUrl Wallabag instance url
28 * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE
30 public function __construct($wallabagUrl, $logoPath)
32 $this->wallabagUrl
= $wallabagUrl;
33 $this->logoPath
= $logoPath;
39 * @param array|Entry $entries An array of entries or one entry
41 public function setEntries($entries)
43 if (!is_array($entries)) {
44 $this->language
= $entries->getLanguage();
45 $entries = array($entries);
48 $this->entries
= $entries;
50 foreach ($entries as $entry) {
51 $this->tags
[] = $entry->getTags();
58 * Sets the category of which we want to get articles, or just one entry.
60 * @param string $method Method to get articles
62 public function updateTitle($method)
64 $this->title
= $method.' articles';
66 if ('entry' === $method) {
67 $this->title
= $this->entries
[0]->getTitle();
74 * Sets the output format.
76 * @param string $format
78 public function exportAs($format)
82 return $this->produceEpub();
85 return $this->produceMobi();
88 return $this->producePDF();
91 return $this->produceCSV();
94 return $this->produceJSON();
97 return $this->produceXML();
100 throw new \
InvalidArgumentException(sprintf('The format "%s" is not yet supported.', $format));
104 * Use PHPePub to dump a .epub file.
106 private function produceEpub()
109 * Start and End of the book
112 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
113 ."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
115 ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
116 ."<title>wallabag articles book</title>\n"
120 $bookEnd = "</body>\n</html>\n";
122 $book = new EPub(EPub
::BOOK_VERSION_EPUB3
);
128 $book->setTitle($this->title
);
129 // Could also be the ISBN number, prefered for published books, or a UUID.
130 $book->setIdentifier($this->title
, EPub
::IDENTIFIER_URI
);
131 // 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.
132 $book->setLanguage($this->language
);
133 $book->setDescription('Some articles saved on my wallabag');
135 foreach ($this->authors
as $author) {
136 $book->setAuthor($author, $author);
139 // I hope this is a non existant address :)
140 $book->setPublisher('wallabag', 'wallabag');
141 // Strictly not needed as the book date defaults to time().
142 $book->setDate(time());
143 $book->setSourceURL($this->wallabagUrl
);
145 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'PHP');
146 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'wallabag');
151 if (file_exists($this->logoPath
)) {
152 $book->setCoverImage('Cover.png', file_get_contents($this->logoPath
), 'image/png');
155 $book->addChapter('Notices', 'Cover2.html', $content_start.$this->getExportInformation('PHPePub').$bookEnd);
160 * Adding actual entries
163 // set tags as subjects
164 foreach ($this->entries
as $entry) {
165 foreach ($this->tags
as $tag) {
166 $book->setSubject($tag['value']);
169 $chapter = $content_start.$entry->getContent().$bookEnd;
170 $book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub
::EXTERNAL_REF_ADD
);
173 return Response
::create(
177 'Content-Description' => 'File Transfer',
178 'Content-type' => 'application/epub+zip',
179 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.epub"',
180 'Content-Transfer-Encoding' => 'binary',
186 * Use PHPMobi to dump a .mobi file.
188 private function produceMobi()
191 $content = new \
MOBIFile();
196 $content->set('title', $this->title
);
197 $content->set('author', implode($this->authors
));
198 $content->set('subject', $this->title
);
203 $content->appendParagraph($this->getExportInformation('PHPMobi'));
204 if (file_exists($this->logoPath
)) {
205 $content->appendImage(imagecreatefrompng($this->logoPath
));
207 $content->appendPageBreak();
210 * Adding actual entries
212 foreach ($this->entries
as $entry) {
213 $content->appendChapterTitle($entry->getTitle());
214 $content->appendParagraph($entry->getContent());
215 $content->appendPageBreak();
217 $mobi->setContentProvider($content);
219 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
220 $this->title
= preg_replace('/[^A-Za-z0-9\-]/', '', $this->title
);
222 return Response
::create(
226 'Accept-Ranges' => 'bytes',
227 'Content-Description' => 'File Transfer',
228 'Content-type' => 'application/x-mobipocket-ebook',
229 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.mobi"',
230 'Content-Transfer-Encoding' => 'binary',
236 * 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 ($this->tags
as $tag) {
264 $pdf->SetKeywords($tag['value']);
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.
292 private function produceCSV()
296 $handle = fopen('php://memory', 'rb+');
298 fputcsv($handle, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'), $delimiter, $enclosure);
300 foreach ($this->entries
as $entry) {
306 $entry->getContent(),
307 implode(', ', $entry->getTags()->toArray()),
308 $entry->getMimetype(),
309 $entry->getLanguage(),
317 $output = stream_get_contents($handle);
320 return Response
::create(
324 'Content-type' => 'application/csv',
325 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.csv"',
326 'Content-Transfer-Encoding' => 'UTF-8',
331 private function produceJSON()
333 return Response
::create(
334 $this->prepareSerializingContent('json'),
337 'Content-type' => 'application/json',
338 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.json"',
339 'Content-Transfer-Encoding' => 'UTF-8',
344 private function produceXML()
346 return Response
::create(
347 $this->prepareSerializingContent('xml'),
350 'Content-type' => 'application/xml',
351 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.xml"',
352 'Content-Transfer-Encoding' => 'UTF-8',
358 * Return a Serializer object for producing processes that need it (JSON & XML).
362 private function prepareSerializingContent($format)
364 $serializer = SerializerBuilder
::create()->build();
366 return $serializer->serialize($this->entries
, $format, SerializationContext
::create()->setGroups(array('entries_for_user')));
370 * Return a kind of footer / information for the epub.
372 * @param string $type Generator of the export, can be: tdpdf, PHPePub, PHPMobi
376 private function getExportInformation($type)
378 $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate
);
380 if ('tcpdf' === $type) {
381 return str_replace('%IMAGE%', '<img src="'.$this->logoPath
.'" />', $info);
384 return str_replace('%IMAGE%', '', $info);