3 namespace Wallabag\CoreBundle\Helper
;
6 use PHPePub\Core\Structure\OPF\DublinCore
;
14 private $authors = array('wallabag');
18 public function __construct($entries)
20 $this->entries
= $entries;
22 foreach ($entries as $entry) {
23 $this->tags
[] = $entry->getTags();
25 if (count($entries) === 1) {
26 $this->language
= $entries[0]->getLanguage();
31 * Sets the category of which we want to get articles, or just one entry.
33 * @param string $method Method to get articles
35 public function setMethod($method)
37 $this->method
= $method;
39 switch ($this->method
) {
41 $this->title
= 'All Articles';
44 $this->title
= 'Unread articles';
47 $this->title
= 'Starred articles';
50 $this->title
= 'Archived articles';
53 $this->title
= $this->entries
[0]->getTitle();
61 * Sets the output format.
63 * @param string $format
65 public function exportAs($format)
67 $this->format
= $format;
69 switch ($this->format
) {
91 private function produceEpub()
94 * Start and End of the book
97 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
98 ."<html xmlns=\"http://www.w3.org/1999/xhtml\" xmlns:epub=\"http://www.idpf.org/2007/ops\">\n"
100 ."<meta http-equiv=\"Default-Style\" content=\"text/html; charset=utf-8\" />\n"
101 .'<title>'._('wallabag articles book')."</title>\n"
105 $bookEnd = "</body>\n</html>\n";
107 $book = new EPub(EPub
::BOOK_VERSION_EPUB3
);
113 $book->setTitle($this->title
);
114 $book->setIdentifier($this->title
, EPub
::IDENTIFIER_URI
); // Could also be the ISBN number, prefered for published books, or a UUID.
115 $book->setLanguage($this->language
); // 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.
116 $book->setDescription(_('Some articles saved on my wallabag'));
118 foreach ($this->authors
as $author) {
119 $book->setAuthor($author, $author);
122 $book->setPublisher('wallabag', 'wallabag'); // I hope this is a non existant address :)
123 $book->setDate(time()); // Strictly not needed as the book date defaults to time().
124 $book->setSourceURL("http://$_SERVER[HTTP_HOST]");
126 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'PHP');
127 $book->addDublinCoreMetadata(DublinCore
::CONTRIBUTOR
, 'wallabag');
133 $book->setCoverImage('Cover.png', file_get_contents('themes/_global/img/appicon/apple-touch-icon-152.png'), 'image/png');
135 $cover = $content_start.'<div style="text-align:center;"><p>'._('Produced by wallabag with PHPePub').'</p><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></div>'.$bookEnd;
137 $book->addChapter('Notices', 'Cover2.html', $cover);
142 * Adding actual entries
145 foreach ($this->entries
as $entry) { //set tags as subjects
146 foreach ($this->tags
as $tag) {
147 $book->setSubject($tag['value']);
150 $chapter = $content_start.$entry->getContent().$bookEnd;
151 $book->addChapter($entry->getTitle(), htmlspecialchars($entry->getTitle()).'.html', $chapter, true, EPub
::EXTERNAL_REF_ADD
);
154 $book->sendBook($this->title
);
157 private function produceMobi()
160 $content = new \
MOBIFile();
166 $content->set('title', $this->title
);
167 $content->set('author', implode($this->authors
));
168 $content->set('subject', $this->title
);
174 $content->appendParagraph('<div style="text-align:center;" ><p>'._('Produced by wallabag with PHPMobi').'</p><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></div>');
175 $content->appendImage(imagecreatefrompng('themes/_global/img/appicon/apple-touch-icon-152.png'));
176 $content->appendPageBreak();
179 * Adding actual entries
182 foreach ($this->entries
as $entry) {
183 $content->appendChapterTitle($entry->getTitle());
184 $content->appendParagraph($entry->getContent());
185 $content->appendPageBreak();
187 $mobi->setContentProvider($content);
189 // the browser inside Kindle Devices doesn't likes special caracters either, we limit to A-z/0-9
190 $this->title
= preg_replace('/[^A-Za-z0-9\-]/', '', $this->title
);
192 // we offer file to download
193 $mobi->download($this->title
.'.mobi');
196 private function producePDF()
198 $pdf = new \
TCPDF(PDF_PAGE_ORIENTATION
, PDF_UNIT
, PDF_PAGE_FORMAT
, true, 'UTF-8', false);
204 $pdf->SetCreator(PDF_CREATOR
);
205 $pdf->SetAuthor('wallabag');
206 $pdf->SetTitle($this->title
);
207 $pdf->SetSubject('Articles via wallabag');
208 $pdf->SetKeywords('wallabag');
215 $intro = '<h1>'.$this->title
.'</h1><div style="text-align:center;" >
216 <p>'._('Produced by wallabag with tcpdf').'</p>
217 <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>
218 <img src="themes/_global/img/appicon/apple-touch-icon-152.png" /></div>';
220 $pdf->writeHTMLCell(0, 0, '', '', $intro, 0, 1, 0, true, '', true);
223 * Adding actual entries
226 foreach ($this->entries
as $entry) {
227 foreach ($this->tags
as $tag) {
228 $pdf->SetKeywords($tag['value']);
232 $html = '<h1>'.$entry->getTitle().'</h1>';
233 $html .= $entry->getContent();
234 $pdf->writeHTMLCell(0, 0, '', '', $html, 0, 1, 0, true, '', true);
237 // set image scale factor
238 $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO
);
240 $pdf->Output($this->title
.'.pdf', 'D');
243 private function produceCSV()
245 header('Content-type: application/csv');
246 header('Content-Disposition: attachment; filename="'.$this->title
.'.csv"');
247 header('Content-Transfer-Encoding: UTF-8');
249 $output = fopen('php://output', 'a');
251 fputcsv($output, array('Title', 'URL', 'Content', 'Tags', 'MIME Type', 'Language'));
252 foreach ($this->entries
as $entry) {
253 fputcsv($output, array($entry->getTitle(),
255 $entry->getContent(),
256 implode(', ', $entry->getTags()->toArray()),
257 $entry->getMimetype(),
258 $entry->getLanguage(), ));