Python 如何自动和手动保存prestashop发票?
我想自动打印发票(pdf),即最近保存在服务器上的内容。并使手动保存成为可能Python 如何自动和手动保存prestashop发票?,python,pdf-generation,prestashop,prestashop-1.6,invoice,Python,Pdf Generation,Prestashop,Prestashop 1.6,Invoice,我想自动打印发票(pdf),即最近保存在服务器上的内容。并使手动保存成为可能 class PDF extends PDFCore { public function render($display = true) { if($this->template == PDF::TEMPLATE_INVOICE) parent::render('F', true); return parent::render($displa
class PDF extends PDFCore
{
public function render($display = true)
{
if($this->template == PDF::TEMPLATE_INVOICE)
parent::render('F', true);
return parent::render($display);
}
}
?>
我使用的是prestashop 1.6.1,发票大部分是从prestashop管理页面下载的,但我需要更简单的方法来打印这些发票,所以我为自己制作了一个adminpage,看起来像这样:
打印机按钮具有发票生成地址的href
比如:“
我可以从这个链接下载它,然后在pdf阅读器中打开它时打印出来,但我想一次点击就可以完成
苏。。。我制作了一个脚本,当pdf保存在某个特定位置时,可以自动打印它
#! /usr/bin/python import os
import os
import time
import os.path
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
class ExampleHandler(FileSystemEventHandler):
def on_created(self, event):
output=str(event.src_path.replace("./",""))
print(output)
#print event.src_path.replace("./","")
print "Got event for file %s" % event.src_path
os.system("lp -d HL2250DN %s" % output)
observer = Observer()
event_handler = ExampleHandler()
observer.schedule(event_handler, path='.',recursive=False)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
有两个选项可将其自动下载到服务器
1。重写PDF.php和PDFGenerator.php文件,如下所示:
class PDF extends PDFCore
{
public function render($display = true)
{
if($this->template == PDF::TEMPLATE_INVOICE)
parent::render('F', true);
return parent::render($display);
}
}
?>
PDF.php
class PDF extends PDFCore
{
public function render($display = true)
{
if($this->template == PDF::TEMPLATE_INVOICE)
parent::render('F', true);
return parent::render($display);
}
}
?>
PDFGenerator.php
class PDF extends PDFCore
{
public function render($display = true)
{
if($this->template == PDF::TEMPLATE_INVOICE)
parent::render('F', true);
return parent::render($display);
}
}
?>
<?php
class PDFGenerator extends PDFGeneratorCore
{
public function render($filename, $display = true)
{
if (empty($filename)) {
throw new PrestaShopException('Missing filename.');
}
$this->lastPage();
if ($display === true) {
$output = 'D';
} elseif ($display === false) {
$output = 'S';
} elseif ($display == 'D') {
$output = 'D';
} elseif ($display == 'S') {
$output = 'S';
} elseif ($display == 'F') {
$output = 'F';
$filename = '/folder/for/print_it/'.str_replace("#", "", $filename);
} else {
$output = 'I';
}
return $this->output($filename, $output);
}
}
?>
所以这个选项的问题是。。如何将href(从打印机按钮单击的内容)传递到url
解决这个问题真的很令人沮丧,我知道有一个简单易行的选择,但我仍然在寻找它。每次生成发票PDF时,都会强制将其保存为本地文件 您要做的是向print按钮添加一个额外的GET参数,并检查它是否存在于被重写的类中,以便PDF仅在您想要直接打印时存储为本地文件 因此,首先为打印按钮添加一个GET参数,例如
&print=1
。无论是在模板中还是在生成这些按钮的任何位置,按钮的href如下所示:
http://www.example.com/admin/index.php?controller=AdminPdf&submitAction=generateInvoicePDF&id_order=3230&print=1
现在,您可以检查PDF
类中是否存在参数,然后才强制将PDF输出到本地文件
类PDF扩展了PDFCore
{
公共函数呈现($display=true)
{
如果($this->template==PDF::template\u发票和工具::getValue('print')==1){
//将PDF输出到本地文件
父::render('F');
//重定向回同一页,这样您就不会得到空白页
工具::重定向管理(Context::getContext()->link->getAdminLink('AdminMyController');
}
否则{
返回父级::render($display);
}
}
}
您可以保持被重写的PDFGenerator
类的原样