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 = 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 Config $craueConfig CraueConfig instance to get wallabag instance url from database
32 * @param string $logoPath Path to the logo FROM THE BUNDLE SCOPE
34 public function __construct(Config
$craueConfig, $logoPath)
36 $this->wallabagUrl
= $craueConfig->get('wallabag_url');
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 // remove new line to avoid crazy results
311 str_replace(array("\r\n", "\r", "\n"), '', $entry->getContent()),
312 implode(', ', $entry->getTags()->toArray()),
313 $entry->getMimetype(),
314 $entry->getLanguage(),
322 $output = stream_get_contents($handle);
325 return Response
::create(
329 'Content-type' => 'application/csv',
330 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.csv"',
331 'Content-Transfer-Encoding' => 'UTF-8',
336 private function produceJSON()
338 return Response
::create(
339 $this->prepareSerializingContent('json'),
342 'Content-type' => 'application/json',
343 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.json"',
344 'Content-Transfer-Encoding' => 'UTF-8',
349 private function produceXML()
351 return Response
::create(
352 $this->prepareSerializingContent('xml'),
355 'Content-type' => 'application/xml',
356 'Content-Disposition' => 'attachment; filename="'.$this->title
.'.xml"',
357 'Content-Transfer-Encoding' => 'UTF-8',
363 * Return a Serializer object for producing processes that need it (JSON & XML).
367 private function prepareSerializingContent($format)
369 $serializer = SerializerBuilder
::create()->build();
371 return $serializer->serialize(
374 SerializationContext
::create()->setGroups(array('entries_for_user'))
379 * Return a kind of footer / information for the epub.
381 * @param string $type Generator of the export, can be: tdpdf, PHPePub, PHPMobi
385 private function getExportInformation($type)
387 $info = str_replace('%EXPORT_METHOD%', $type, $this->footerTemplate
);
389 if ('tcpdf' === $type) {
390 return str_replace('%IMAGE%', '<img src="'.$this->logoPath
.'" />', $info);
393 return str_replace('%IMAGE%', '', $info);