我想自动打印发票(pdf),最近保存在服务器上的内容。并且还可以手动保存
我使用的是 prestashop 1.6.1,发票大多是从 prestashop 管理页面下载的,但我需要更简单的方法来打印这些发票,所以我为自己制作了一个管理页面,它看起来像这样:
打印机按钮具有发票生成地址的 href,例如:“ http://www.example.com/admin/index.php?controller=AdminPdf&submitAction=generateInvoicePDF&id_order=3230 ”
从链接我可以下载它,然后在 pdf 阅读器中打开它时打印它,但我想一键完成。
Soo ...我制作了一个脚本,用于在保存在某个特定位置时自动打印 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 文件:
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);
}
}
?>
PDF生成器.php
<?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);
}
}
?>
2.使用脚本下载
第一个选项适用于自动保存,但是当我尝试手动保存发票时,我得到了一个空白或损坏的 pdf 文件。我也尝试更改 pdf.php,但它对我没有用。还发表了一篇关于此的帖子:Prestashop 手动和自动保存发票。没有给出答案,我继续选择第二个选项。
我尝试使用 python 脚本下载发票并且它有效,但我怎么知道要下载哪个?
#!/usr/bin/env python
import requests
import webbrowser
url = "http://www.example.com/admin/index.php?controller=AdminLogin&token=5a01dc4e606bca6c26e95ddea92d3d15"
url2 = "http://www.example.com/admin/index.php?controller=AdminPdf&token=35b276c05aa6f5eb516737a8d534eb66&submitAction=generateInvoicePDF&id_order=3221"
payload = {'example': 'example',
'example': 'example',
'stay_logged_in':'2',
'submitLogin':'1',}
with requests.session() as s:
# fetch the login page
s.get(url)
# post to the login form
r = s.post(url, data=payload)
print(r.text)
response = s.get(url2)
with open('/tmp/metadataa.pdf', 'wb') as f:
f.write(response.content)
Soo 这个选项的问题是.. 我怎样才能将 href(从打印机按钮点击的内容)传递给 url?
解决这个问题真的很令人沮丧,我知道有一个简单易行的选择,但我仍在寻找这个。
每次生成发票 PDF 时,您都会强制将其保存为本地文件。
您想要做的是向打印按钮添加一个额外的 GET 参数,并检查它是否存在于覆盖类中,以便 PDF 仅在您想直接打印时才存储为本地文件。
所以首先添加一个 GET 参数来打印按钮,例如。&print=1
. 无论是在您的模板中还是在您生成这些按钮的任何地方,按钮的 href 如下所示:
http://www.example.com/admin/index.php?controller=AdminPdf&submitAction=generateInvoicePDF&id_order=3230&print=1
现在您可以检查PDF
类中是否存在参数,然后才强制将 PDF 输出到本地文件。
class PDF extends PDFCore
{
public function render($display = true)
{
if($this->template == PDF::TEMPLATE_INVOICE && Tools::getValue('print') == 1) {
// Output PDF to local file
parent::render('F');
// Redirect back to the same page so you don't get blank page
Tools::redirectAdmin(Context::getContext()->link->getAdminLink('AdminMyController'));
}
else {
return parent::render($display);
}
}
}
您可以PDFGenerator
按原样保留覆盖类。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句