安装pageoffice什么的就不说了,拿到官方demo如何与thinkphp对接
以下是创建,只读,表单,签名盖章的示例。粗糙的封装了一下
新建个控制器类
<?php
namespace app\index\controller;
use think\Controller;
use JavaC;
class Index extends Controller
{
public function index()
{
return $this->fetch();
}
public function two()
{
require './SimpleWord/Word.php';
}
public function one()
{
$java=new JavaC;
return $java->open('\\st\\test.doc');
}
public function SaveFile()
{
$java=new JavaC;
$java->SaveFile('st\\');
}
public function SaveNewFile()
{
$java=new JavaC;
$name=uniqid().'.doc';
$java->SaveNewFile('st\\'.$name);
}
public function add()
{
$java=new JavaC;
$name='\\st\\u2_'.uniqid().'.doc';
$java->add($name);
$java->open($name);
}
public function reader()
{
$java=new JavaC;
$name='\\st\\test.doc';
return $java->reader($name);
}
public function form()
{
$java=new JavaC;
$name='\\st\\form.doc';
return $java->submit($name);
}
public function SaveData()
{
$java=new JavaC;
return $java->SaveData();
}
public function add_zhang()
{
$java=new JavaC;
$name='\\st\\form.doc';
return $java->add_zhang($name);
}
}
extend文件夹下新建JavaC类
JavaC类中仍可以请求参数 如input(),可用于获取id,和word文件名等
<?php
class JavaC
{
protected $PageOfficeCtrl;
protected $OpenMode;
protected $ip;
protected $fs;
protected $path;
public function __construct()
{
$ip = GetHostByName($_SERVER['SERVER_NAME']); //获取本机IP
require_once("http://" . $ip . ":8080/JavaBridge/java/Java.inc");//此行必须
$PageOfficeCtrl = new Java("com.zhuozhengsoft.pageoffice.PageOfficeCtrlPHP");//此行必须
$OpenMode = new Java("com.zhuozhengsoft.pageoffice.OpenModeType");
$fs = new Java("com.zhuozhengsoft.pageoffice.FileSaverPHP");//此行必须
$PageOfficeCtrl->setServerPage("http://" . $ip . ":8080/JavaBridge/poserver.zz");//此行必须,设置服务器页面
java_set_file_encoding("GBK");//设置中文编码,若涉及到中文必须设置中文编码
//打开excel文档
$PageOfficeCtrl->UserAgent = $_SERVER['HTTP_USER_AGENT'];//若使用谷歌浏览器此行代码必须有,其他浏览器此行代码可不加
$path = str_replace('public', '', $_SERVER['DOCUMENT_ROOT']) . 'word/';
$path = realpath($path);
$this->path = $path;
$this->ip = $ip;
$this->fs = $fs;
$this->PageOfficeCtrl = $PageOfficeCtrl;
$this->OpenMode = $OpenMode;
}
public function get($name)
{
return $this->$name;
}
public function open($name)
{
$PageOfficeCtrl = $this->PageOfficeCtrl;
$OpenMode = $this->OpenMode;
$this->_btn($PageOfficeCtrl);
$file_name = $this->path . $name;
$PageOfficeCtrl->setSaveFilePage("SaveFile");//设置保存页面
$PageOfficeCtrl->webOpen($file_name, $OpenMode->docNormalEdit, "aa");//此行必须
return $PageOfficeCtrl->getDocumentView("PageOfficeCtrl1");
}
public function reader($name)
{
$PageOfficeCtrl = $this->PageOfficeCtrl;
$OpenMode = $this->OpenMode;
$this->_btn($PageOfficeCtrl);
$file_name = $this->path . $name;
$this->stop_copy($PageOfficeCtrl);
$PageOfficeCtrl->webOpen($file_name, $OpenMode->docReadOnly, "bb");//只读模式打开word文档,此行必须
return $PageOfficeCtrl->getDocumentView("PageOfficeCtrl1");
}
public function SaveFile($middel_path)
{
$fs = $this->fs;
$fs->load(file_get_contents("php://input"));//此行必须
$file = $this->path . '\\' . $middel_path . $fs->getFileName();
$fs->saveToFile($file); //保存文件
echo $fs->close();//此行必须
}
public function SaveNewFile($file_name)
{
$fs = $this->fs;
$fs->load(file_get_contents("php://input"));//此行必须
$file = $this->path . '\\' . $file_name;
$fs->saveToFile($file); //保存文件
$fs->setCustomSaveResult("ok");
echo $fs->close();//此行必须
}
public function add($name)
{
$filepath=$this->path;
$oldPath=$filepath.'\st\empty.doc';
$newPath=$filepath.$name;
$flg = copy($oldPath, $newPath);
if(!$flg){
throw new Exception('创建失败');
}
}
//提交Word中的用户输入内容
public function submit($name)
{
$PageOfficeCtrl = $this->PageOfficeCtrl;
$OpenMode = $this->OpenMode;
$PageOfficeCtrl->setOfficeToolbars(false);//隐藏Office工具条
$doc = new Java("com.zhuozhengsoft.pageoffice.wordwriter.WordDocument");//声明WordDocument变量
$dataRegion1 = $doc->openDataRegion("PO_userName");//打开数据区域,openDataRegion方法的参数代表Word文档中的书签名称
$dataRegion1->setEditing(true);//设置DataRegion的可编辑性
$dataRegion1->setValue("");//为DataRegion赋值,此处的值可在页面中打开Word文档后自己进行修改
$dataRegion2 = $doc->openDataRegion("PO_deptName");
$dataRegion2->setEditing(true);
$dataRegion2->setValue("");
$PageOfficeCtrl->setWriter($doc);
$this->_btn($PageOfficeCtrl);
$PageOfficeCtrl->setSaveDataPage("SaveData");//设置保存页面
$filepath=$this->path.$name;
$PageOfficeCtrl->webOpen($filepath, $OpenMode->docSubmitForm, "张三");//此行必须
echo $PageOfficeCtrl->getDocumentView("PageOfficeCtrl1");
}
public function _btn($PageOfficeCtrl)
{
//添加自定义按钮
$PageOfficeCtrl->addCustomToolButton("save", "Save", 1);
$PageOfficeCtrl->addCustomToolButton("打印", "PrintFile", 6);
$PageOfficeCtrl->addCustomToolButton("全屏/还原", "IsFullScreen", 4);
$PageOfficeCtrl->addCustomToolButton("-", "", 0);
$PageOfficeCtrl->addCustomToolButton("关闭", "Close", 21);
echo "<script>";
echo 'function IsFullScreen() {
document.getElementById("PageOfficeCtrl1").FullScreen = !document.getElementById("PageOfficeCtrl1").FullScreen;
}
function Save() {
document.getElementById("PageOfficeCtrl1").WebSave();
}
';
echo "</script>";
}
public function stop_copy($PageOfficeCtrl)
{
$PageOfficeCtrl->setAllowCopy(false);//禁止拷贝
$PageOfficeCtrl->setMenubar(false);//隐藏菜单栏
$PageOfficeCtrl->setOfficeToolbars(false);//隐藏Office工具条
$PageOfficeCtrl->setCustomToolbar(false);//隐藏自定义工具栏
$PageOfficeCtrl->setJsFunction_AfterDocumentOpened("AfterDocumentOpened");
echo "<script>";
echo 'function AfterDocumentOpened() {
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(3, false); // 禁止保存
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(4, false); // 禁止另存
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(5, false); //禁止打印
document.getElementById("PageOfficeCtrl1").SetEnableFileCommand(6, false); // 禁止页面设置
}';
echo "</script>";
}
public function SaveData()
{
$doc = new Java("com.zhuozhengsoft.pageoffice.wordreader.WordDocumentPHP");
$doc->load(file_get_contents("php://input"));
$dataUserName = $doc->openDataRegion("PO_userName");
$dataDeptName = $doc->openDataRegion("PO_deptName");
$content = "";
$content .= "<br/>员工姓名:".$dataUserName->getValue();
echo $content .= "<br/>部门名称:".$dataDeptName->getValue();
$doc->showPage(500, 400);//弹窗
echo $doc->close();
}
public function add_zhang($name)
{
$PageOfficeCtrl = $this->PageOfficeCtrl;
$OpenMode = $this->OpenMode;
$PageOfficeCtrl->setMenubar(false);//隐藏菜单栏
$PageOfficeCtrl->setOfficeToolbars(false);//隐藏Office工具条
//添加自定义按钮
$PageOfficeCtrl->addCustomToolButton("加盖印章", "InsertSeal()", 2);
$PageOfficeCtrl->addCustomToolButton("签字", "AddHandSign()", 3);
$PageOfficeCtrl->addCustomToolButton("验证印章", "VerifySeal()", 5);
$PageOfficeCtrl->addCustomToolButton("修改密码", "ChangePsw()", 0);
$PageOfficeCtrl->setSaveFilePage("SaveFile");//设置保存页面
$file_name = $this->path . $name;
$PageOfficeCtrl->webOpen($file_name, $OpenMode->docNormalEdit, "张三");//此行必须
echo '<input type="button" value="设置父窗口Count的值加5,并关闭窗口" onclick="increaseCountAndClose(5);" /></br>';
echo $PageOfficeCtrl->getDocumentView("PageOfficeCtrl1");
echo "<script>";
echo '
function InsertSeal() {
try {
document.getElementById("PageOfficeCtrl1").ZoomSeal.AddSeal();
}
catch (e) { }
}
function AddHandSign() {
document.getElementById("PageOfficeCtrl1").ZoomSeal.AddHandSign();
}
function VerifySeal() {
document.getElementById("PageOfficeCtrl1").ZoomSeal.VerifySeal();
}
function ChangePsw() {
document.getElementById("PageOfficeCtrl1").ZoomSeal.ShowSettingsBox();
}
function increaseCountAndClose(value) {
var sResult = window.external.CallParentFunc("updateCount("+value+");");
window.external.close();
}';
echo "</script>";
}
}
以上是示例,自行加上数据库操作即可
遇见个匪夷所思的问题
echo name); //打印出的是对象
于是只能用$name->__client->arg->val; 获取字符串
$name=$doc->openDataRegion("PO_userName")->getValue(); //dump($name)是对象
echo $name; //这句不能少,想不通
$name=$name->__client->arg->val; //获取到字符串
$name=$this->unicode_encode($name); //转unicode
$data['name']=$this->unicode_decode($name,'UTF-8'); //unicode转utf8
网友评论