add plugin web2print
This commit is contained in:
parent
1f024a7e71
commit
d3b9220931
17 changed files with 596 additions and 2 deletions
10
.gitignore
vendored
10
.gitignore
vendored
|
|
@ -51,12 +51,20 @@ Icon
|
||||||
|
|
||||||
/site/config/.license
|
/site/config/.license
|
||||||
|
|
||||||
|
# Local files
|
||||||
|
# ---------------
|
||||||
/0_local
|
/0_local
|
||||||
|
|
||||||
|
# Managed through composer
|
||||||
|
# ---------------
|
||||||
/kirby
|
/kirby
|
||||||
/vendor
|
/vendor
|
||||||
|
|
||||||
/node_modules
|
/node_modules
|
||||||
|
|
||||||
/content
|
/content
|
||||||
|
|
||||||
|
# Claude settings
|
||||||
|
# ---------------
|
||||||
|
.claude
|
||||||
|
|
||||||
|
|
|
||||||
20
site/plugins/web2print/.editorconfig
Normal file
20
site/plugins/web2print/.editorconfig
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
# This file is for unifying the coding style for different editors and IDEs
|
||||||
|
# editorconfig.org
|
||||||
|
|
||||||
|
[*]
|
||||||
|
charset = utf-8
|
||||||
|
indent_style = space
|
||||||
|
indent_size = 2
|
||||||
|
end_of_line = lf
|
||||||
|
insert_final_newline = true
|
||||||
|
trim_trailing_whitespace = true
|
||||||
|
|
||||||
|
[*.php]
|
||||||
|
indent_size = 4
|
||||||
|
|
||||||
|
[*.md,*.txt]
|
||||||
|
trim_trailing_whitespace = false
|
||||||
|
insert_final_newline = false
|
||||||
|
|
||||||
|
[composer.json]
|
||||||
|
indent_size = 4
|
||||||
11
site/plugins/web2print/.gitattributes
vendored
Normal file
11
site/plugins/web2print/.gitattributes
vendored
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
# Note: You need to uncomment the lines you want to use; the other lines can be deleted
|
||||||
|
|
||||||
|
# Git
|
||||||
|
# .gitattributes export-ignore
|
||||||
|
# .gitignore export-ignore
|
||||||
|
|
||||||
|
# Tests
|
||||||
|
# /.coveralls.yml export-ignore
|
||||||
|
# /.travis.yml export-ignore
|
||||||
|
# /phpunit.xml.dist export-ignore
|
||||||
|
# /tests/ export-ignore
|
||||||
14
site/plugins/web2print/.gitignore
vendored
Normal file
14
site/plugins/web2print/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,14 @@
|
||||||
|
# OS files
|
||||||
|
.DS_Store
|
||||||
|
|
||||||
|
# npm modules
|
||||||
|
/node_modules
|
||||||
|
|
||||||
|
# Parcel cache folder
|
||||||
|
.cache
|
||||||
|
|
||||||
|
# Composer files
|
||||||
|
/vendor
|
||||||
|
|
||||||
|
# kirbyup temp development entry
|
||||||
|
/index.dev.mjs
|
||||||
21
site/plugins/web2print/LICENSE.md
Normal file
21
site/plugins/web2print/LICENSE.md
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
MIT License
|
||||||
|
|
||||||
|
Copyright (c) <Year> <Your Name>
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||||
|
of this software and associated documentation files (the "Software"), to deal
|
||||||
|
in the Software without restriction, including without limitation the rights
|
||||||
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||||
|
copies of the Software, and to permit persons to whom the Software is
|
||||||
|
furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||||
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||||
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||||
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||||
|
SOFTWARE.
|
||||||
117
site/plugins/web2print/README.md
Normal file
117
site/plugins/web2print/README.md
Normal file
|
|
@ -0,0 +1,117 @@
|
||||||
|
# Kirby Pluginkit: Example plugin for Kirby
|
||||||
|
|
||||||
|
> Variant "Panel plugin setup"
|
||||||
|
|
||||||
|
This is a boilerplate for a Kirby Panel plugin that can be installed via all three [supported installation methods](https://getkirby.com/docs/guide/plugins/plugin-setup-basic#the-three-plugin-installation-methods).
|
||||||
|
|
||||||
|
You can find a list of Pluginkit variants on the [`master` branch](https://github.com/getkirby/pluginkit/tree/master).
|
||||||
|
|
||||||
|
****
|
||||||
|
|
||||||
|
## How to use the Pluginkit
|
||||||
|
|
||||||
|
1. Fork this repository
|
||||||
|
2. Change the plugin name and description in the `composer.json`
|
||||||
|
3. Change the plugin name in the `index.php` and `src/index.js`
|
||||||
|
4. Change the license if you don't want to publish under MIT
|
||||||
|
5. Add your plugin code to the `index.php` and `src/index.js`
|
||||||
|
6. Update this `README` with instructions for your plugin
|
||||||
|
|
||||||
|
### Install the development and build setup
|
||||||
|
|
||||||
|
We use [kirbyup](https://github.com/johannschopplich/kirbyup) for the development and build setup.
|
||||||
|
|
||||||
|
You can start developing directly. kirbyup will be fetched remotely with your first `npm run` command, which may take a short amount of time.
|
||||||
|
|
||||||
|
### Development
|
||||||
|
|
||||||
|
You can start the dev process with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run dev
|
||||||
|
```
|
||||||
|
|
||||||
|
This will automatically update the `index.js` and `index.css` of your plugin as soon as you make changes.
|
||||||
|
Reload the Panel to see your code changes reflected.
|
||||||
|
|
||||||
|
With kirbyup 2.0.0+ and Kirby 3.7.4+ you can alternatively use hot module reloading (HMR):
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run serve
|
||||||
|
```
|
||||||
|
|
||||||
|
This will start a development server that updates the page as soon as you make changes. Some updates are instant, like CSS or Vue template changes, others require a reload of the page, which happens automatically.
|
||||||
|
|
||||||
|
> [!NOTE]
|
||||||
|
> The live reload functionality requires top level await, [which is only supported in modern browsers](https://caniuse.com/mdn-javascript_operators_await_top_level). If you're developing in older browsers, use `npm run dev` and reload the page manually to see changes.
|
||||||
|
|
||||||
|
### Production
|
||||||
|
|
||||||
|
As soon as you are happy with your plugin, you should build the final version with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
npm run build
|
||||||
|
```
|
||||||
|
|
||||||
|
This will automatically create a minified and optimized version of your `index.js` and `index.css`
|
||||||
|
which you can ship with your plugin.
|
||||||
|
|
||||||
|
We have a tutorial on how to build your own plugin based on the Pluginkit [in the Kirby documentation](https://getkirby.com/docs/guide/plugins/plugin-setup-basic).
|
||||||
|
|
||||||
|
### Build reproducibility
|
||||||
|
|
||||||
|
While kirbyup will stay backwards compatible, exact build reproducibility may be of importance to you. If so, we recommend to target a specific package version, rather than using npx:
|
||||||
|
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"scripts": {
|
||||||
|
"dev": "kirbyup src/index.js --watch",
|
||||||
|
"build": "kirbyup src/index.js"
|
||||||
|
},
|
||||||
|
"devDependencies": {
|
||||||
|
"kirbyup": "^3.1.0"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
What follows is an example README for your plugin.
|
||||||
|
|
||||||
|
****
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
### Download
|
||||||
|
|
||||||
|
Download and copy this repository to `/site/plugins/{{ plugin-name }}`.
|
||||||
|
|
||||||
|
### Git submodule
|
||||||
|
|
||||||
|
```bash
|
||||||
|
git submodule add https://github.com/{{ your-name }}/{{ plugin-name }}.git site/plugins/{{ plugin-name }}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Composer
|
||||||
|
|
||||||
|
```bash
|
||||||
|
composer require {{ your-name }}/{{ plugin-name }}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Setup
|
||||||
|
|
||||||
|
*Additional instructions on how to configure the plugin (e.g. blueprint setup, config options, etc.)*
|
||||||
|
|
||||||
|
## Options
|
||||||
|
|
||||||
|
*Document the options and APIs that this plugin offers*
|
||||||
|
|
||||||
|
## Development
|
||||||
|
|
||||||
|
*Add instructions on how to help working on the plugin (e.g. npm setup, Composer dev dependencies, etc.)*
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
MIT
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
- [Your Name](https://github.com/ghost)
|
||||||
18
site/plugins/web2print/SECURITY.md
Normal file
18
site/plugins/web2print/SECURITY.md
Normal file
|
|
@ -0,0 +1,18 @@
|
||||||
|
# Security Policy
|
||||||
|
|
||||||
|
## Supported Versions
|
||||||
|
|
||||||
|
*Use this section to tell people about which versions of your project are currently being supported with security updates.*
|
||||||
|
|
||||||
|
| Version | Supported |
|
||||||
|
| ------- | ------------------ |
|
||||||
|
| 5.1.x | :white_check_mark: |
|
||||||
|
| 5.0.x | :x: |
|
||||||
|
| 4.0.x | :white_check_mark: |
|
||||||
|
| < 4.0 | :x: |
|
||||||
|
|
||||||
|
## Reporting a Vulnerability
|
||||||
|
|
||||||
|
*Use this section to tell people how to report a vulnerability.*
|
||||||
|
|
||||||
|
*Tell them where to go, how often they can expect to get an update on a reported vulnerability, what to expect if the vulnerability is accepted or declined, etc.*
|
||||||
21
site/plugins/web2print/composer.json
Normal file
21
site/plugins/web2print/composer.json
Normal file
|
|
@ -0,0 +1,21 @@
|
||||||
|
{
|
||||||
|
"name": "getkirby/pluginkit",
|
||||||
|
"description": "Kirby Example Plugin",
|
||||||
|
"license": "MIT",
|
||||||
|
"type": "kirby-plugin",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "Your Name",
|
||||||
|
"email": "you@example.com"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"require": {
|
||||||
|
"getkirby/composer-installer": "^1.1"
|
||||||
|
},
|
||||||
|
"config": {
|
||||||
|
"allow-plugins": {
|
||||||
|
"getkirby/composer-installer": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
66
site/plugins/web2print/composer.lock
generated
Normal file
66
site/plugins/web2print/composer.lock
generated
Normal file
|
|
@ -0,0 +1,66 @@
|
||||||
|
{
|
||||||
|
"_readme": [
|
||||||
|
"This file locks the dependencies of your project to a known state",
|
||||||
|
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||||
|
"This file is @generated automatically"
|
||||||
|
],
|
||||||
|
"content-hash": "37a8e61308b9b6f49cb9835f477f0c64",
|
||||||
|
"packages": [
|
||||||
|
{
|
||||||
|
"name": "getkirby/composer-installer",
|
||||||
|
"version": "1.2.1",
|
||||||
|
"source": {
|
||||||
|
"type": "git",
|
||||||
|
"url": "https://github.com/getkirby/composer-installer.git",
|
||||||
|
"reference": "c98ece30bfba45be7ce457e1102d1b169d922f3d"
|
||||||
|
},
|
||||||
|
"dist": {
|
||||||
|
"type": "zip",
|
||||||
|
"url": "https://api.github.com/repos/getkirby/composer-installer/zipball/c98ece30bfba45be7ce457e1102d1b169d922f3d",
|
||||||
|
"reference": "c98ece30bfba45be7ce457e1102d1b169d922f3d",
|
||||||
|
"shasum": ""
|
||||||
|
},
|
||||||
|
"require": {
|
||||||
|
"composer-plugin-api": "^1.0 || ^2.0"
|
||||||
|
},
|
||||||
|
"require-dev": {
|
||||||
|
"composer/composer": "^1.8 || ^2.0"
|
||||||
|
},
|
||||||
|
"type": "composer-plugin",
|
||||||
|
"extra": {
|
||||||
|
"class": "Kirby\\ComposerInstaller\\Plugin"
|
||||||
|
},
|
||||||
|
"autoload": {
|
||||||
|
"psr-4": {
|
||||||
|
"Kirby\\": "src/"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"notification-url": "https://packagist.org/downloads/",
|
||||||
|
"license": [
|
||||||
|
"MIT"
|
||||||
|
],
|
||||||
|
"description": "Kirby's custom Composer installer for the Kirby CMS and for Kirby plugins",
|
||||||
|
"homepage": "https://getkirby.com",
|
||||||
|
"support": {
|
||||||
|
"issues": "https://github.com/getkirby/composer-installer/issues",
|
||||||
|
"source": "https://github.com/getkirby/composer-installer/tree/1.2.1"
|
||||||
|
},
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"url": "https://getkirby.com/buy",
|
||||||
|
"type": "custom"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"time": "2020-12-28T12:54:39+00:00"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"packages-dev": [],
|
||||||
|
"aliases": [],
|
||||||
|
"minimum-stability": "stable",
|
||||||
|
"stability-flags": [],
|
||||||
|
"prefer-stable": false,
|
||||||
|
"prefer-lowest": false,
|
||||||
|
"platform": [],
|
||||||
|
"platform-dev": [],
|
||||||
|
"plugin-api-version": "2.6.0"
|
||||||
|
}
|
||||||
0
site/plugins/web2print/index.css
Normal file
0
site/plugins/web2print/index.css
Normal file
1
site/plugins/web2print/index.js
Normal file
1
site/plugins/web2print/index.js
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
(function(){"use strict";function f(t,n,e,i,r,s,g,a){var o=typeof t=="function"?t.options:t;return n&&(o.render=n,o.staticRenderFns=e,o._compiled=!0),{exports:t,options:o}}const l={__name:"Web2PrintBtn",props:{htmlPageString:String,cssPath:String,printFormat:String,pageId:String},setup(t){const{htmlPageString:n,cssPath:e,printFormat:i,pageId:r}=t;async function s(){const a={method:"POST",body:JSON.stringify({html:n,cssPath:e,printFormat:i,pageId:r})},c=await(await fetch("/web2print.json",a)).json();console.log(c),c.success&&window.panel.view.reload()}return{__sfc:!0,getPdf:s}}};var u=function(){var n=this,e=n._self._c,i=n._self._setupProxy;return e("k-button",{attrs:{variant:"filled"},on:{click:function(r){return i.getPdf()}}},[n._v("Générer")])},_=[],d=f(l,u,_);const p=d.exports;window.panel.plugin("studio-variable/web2print",{fields:{web2print:p}})})();
|
||||||
33
site/plugins/web2print/index.php
Normal file
33
site/plugins/web2print/index.php
Normal file
|
|
@ -0,0 +1,33 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
Kirby::plugin('studio-variable/web2print', [
|
||||||
|
'fields' => [
|
||||||
|
'web2print' => [
|
||||||
|
'props' => [
|
||||||
|
'cssPath' => function($cssPath = null) {
|
||||||
|
return $cssPath;
|
||||||
|
}
|
||||||
|
],
|
||||||
|
'computed' => [
|
||||||
|
'htmlPageString' => function() {
|
||||||
|
return $this->model()->render();
|
||||||
|
},
|
||||||
|
'cssPath' => function() {
|
||||||
|
return $this->cssPath ?? 'assets/css/style.css';
|
||||||
|
},
|
||||||
|
'printFormat' => function() {
|
||||||
|
return $this->model()->printFormat()->value() ?? 'A4';
|
||||||
|
},
|
||||||
|
'pageId' => function() {
|
||||||
|
return $this->model()->id();
|
||||||
|
}
|
||||||
|
]
|
||||||
|
]
|
||||||
|
],
|
||||||
|
'routes' => [
|
||||||
|
require __DIR__ . '/src/routes/web2print.php',
|
||||||
|
],
|
||||||
|
'blueprints' => [
|
||||||
|
'tabs/web2print' => __DIR__ . '/src/blueprints/tabs/web2print.yml'
|
||||||
|
]
|
||||||
|
]);
|
||||||
12
site/plugins/web2print/package.json
Normal file
12
site/plugins/web2print/package.json
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
{
|
||||||
|
"name": "getkirby/pluginkit",
|
||||||
|
"description": "Kirby Example Plugin",
|
||||||
|
"license": "MIT",
|
||||||
|
"type": "kirby-plugin",
|
||||||
|
"version": "1.0.0",
|
||||||
|
"scripts": {
|
||||||
|
"dev": "npx -y kirbyup src/index.js --watch",
|
||||||
|
"serve": "npx -y kirbyup serve src/index.js",
|
||||||
|
"build": "npx -y kirbyup src/index.js"
|
||||||
|
}
|
||||||
|
}
|
||||||
20
site/plugins/web2print/src/blueprints/tabs/web2print.yml
Normal file
20
site/plugins/web2print/src/blueprints/tabs/web2print.yml
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
label: web2print
|
||||||
|
icon: print
|
||||||
|
sections:
|
||||||
|
web2printSection:
|
||||||
|
type: fields
|
||||||
|
fields:
|
||||||
|
printFormat:
|
||||||
|
label: Format
|
||||||
|
type: select
|
||||||
|
options:
|
||||||
|
- A4
|
||||||
|
- A5
|
||||||
|
width: 1/4
|
||||||
|
generatePdfBtn:
|
||||||
|
type: web2print
|
||||||
|
width: 1/4
|
||||||
|
generatedPdfs:
|
||||||
|
label: PDF générés
|
||||||
|
type: files
|
||||||
|
width: 1/4
|
||||||
36
site/plugins/web2print/src/components/Web2PrintBtn.vue
Normal file
36
site/plugins/web2print/src/components/Web2PrintBtn.vue
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
<template>
|
||||||
|
<k-button @click="getPdf()" variant="filled">Générer</k-button>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup>
|
||||||
|
const { htmlPageString, cssPath, printFormat, pageId } = defineProps({
|
||||||
|
htmlPageString: String,
|
||||||
|
cssPath: String,
|
||||||
|
printFormat: String,
|
||||||
|
pageId: String,
|
||||||
|
});
|
||||||
|
|
||||||
|
async function getPdf() {
|
||||||
|
const html = htmlPageString;
|
||||||
|
|
||||||
|
const requestConfig = {
|
||||||
|
method: "POST",
|
||||||
|
body: JSON.stringify({
|
||||||
|
html,
|
||||||
|
cssPath,
|
||||||
|
printFormat,
|
||||||
|
pageId,
|
||||||
|
}),
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch("/web2print.json", requestConfig);
|
||||||
|
const json = await response.json();
|
||||||
|
|
||||||
|
console.log(json);
|
||||||
|
|
||||||
|
if (json.success) {
|
||||||
|
// Recharger la page pour afficher le nouveau PDF
|
||||||
|
window.panel.view.reload();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
</script>
|
||||||
7
site/plugins/web2print/src/index.js
Normal file
7
site/plugins/web2print/src/index.js
Normal file
|
|
@ -0,0 +1,7 @@
|
||||||
|
import Web2PrintBtn from "./components/Web2PrintBtn.vue";
|
||||||
|
|
||||||
|
window.panel.plugin("studio-variable/web2print", {
|
||||||
|
fields: {
|
||||||
|
web2print: Web2PrintBtn,
|
||||||
|
},
|
||||||
|
});
|
||||||
189
site/plugins/web2print/src/routes/web2print.php
Normal file
189
site/plugins/web2print/src/routes/web2print.php
Normal file
|
|
@ -0,0 +1,189 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
// Fonction pour résoudre les @import CSS récursivement
|
||||||
|
function resolveCssImports($cssPath) {
|
||||||
|
if (!file_exists($cssPath)) {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
|
||||||
|
$css = file_get_contents($cssPath);
|
||||||
|
$baseDir = dirname($cssPath);
|
||||||
|
|
||||||
|
// Trouver tous les @import
|
||||||
|
$css = preg_replace_callback('/@import\s+url\(["\']?([^"\')]+)["\']?\);?/', function($matches) use ($baseDir) {
|
||||||
|
$importPath = $matches[1];
|
||||||
|
$fullPath = $baseDir . '/' . $importPath;
|
||||||
|
return resolveCssImports($fullPath);
|
||||||
|
}, $css);
|
||||||
|
|
||||||
|
return $css;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'pattern' => '/web2print.json',
|
||||||
|
'method' => 'POST',
|
||||||
|
'action' => function () {
|
||||||
|
header('Content-Type: application/json');
|
||||||
|
|
||||||
|
$jsonRequest = file_get_contents('php://input');
|
||||||
|
$body = json_decode($jsonRequest);
|
||||||
|
|
||||||
|
if (!$body || !isset($body->html)) {
|
||||||
|
http_response_code(400);
|
||||||
|
return json_encode(['error' => 'Missing html parameter']);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isset($body->pageId) || empty($body->pageId)) {
|
||||||
|
http_response_code(400);
|
||||||
|
return json_encode(['error' => 'Missing pageId parameter']);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Récupérer le cssPath depuis le body ou utiliser la valeur par défaut
|
||||||
|
$cssPath = isset($body->cssPath) && !empty($body->cssPath)
|
||||||
|
? $body->cssPath
|
||||||
|
: 'assets/css/style.css';
|
||||||
|
|
||||||
|
// Récupérer le format d'impression
|
||||||
|
$printFormat = isset($body->printFormat) && !empty($body->printFormat)
|
||||||
|
? $body->printFormat
|
||||||
|
: 'A4';
|
||||||
|
|
||||||
|
// Récupérer la page Kirby
|
||||||
|
$page = kirby()->page($body->pageId);
|
||||||
|
if (!$page) {
|
||||||
|
http_response_code(404);
|
||||||
|
return json_encode(['error' => 'Page not found']);
|
||||||
|
}
|
||||||
|
|
||||||
|
$ch = curl_init('https://web2print.studio-variable.com/generate');
|
||||||
|
|
||||||
|
$html = $body->html;
|
||||||
|
|
||||||
|
// Nettoyer le HTML pour la génération PDF
|
||||||
|
$dom = new DOMDocument();
|
||||||
|
@$dom->loadHTML('<?xml encoding="UTF-8">' . $html, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
|
||||||
|
|
||||||
|
// Supprimer tous les scripts
|
||||||
|
$scripts = $dom->getElementsByTagName('script');
|
||||||
|
while ($scripts->length > 0) {
|
||||||
|
$scripts->item(0)->parentNode->removeChild($scripts->item(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Supprimer les attributs Alpine.js (x-data, x-show, @click, etc.)
|
||||||
|
$xpath = new DOMXPath($dom);
|
||||||
|
$elements = $xpath->query('//*[@*[starts-with(name(), "x-") or starts-with(name(), "@") or starts-with(name(), ":")]]');
|
||||||
|
foreach ($elements as $element) {
|
||||||
|
$attributesToRemove = [];
|
||||||
|
foreach ($element->attributes as $attr) {
|
||||||
|
if (strpos($attr->name, 'x-') === 0 || strpos($attr->name, '@') === 0 || strpos($attr->name, ':') === 0) {
|
||||||
|
$attributesToRemove[] = $attr->name;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach ($attributesToRemove as $attrName) {
|
||||||
|
$element->removeAttribute($attrName);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Supprimer les liens CSS et les remplacer par un style inline
|
||||||
|
$head = $dom->getElementsByTagName('head')->item(0);
|
||||||
|
if ($head) {
|
||||||
|
// Supprimer tous les <link rel="stylesheet">
|
||||||
|
$links = $dom->getElementsByTagName('link');
|
||||||
|
$linksToRemove = [];
|
||||||
|
foreach ($links as $link) {
|
||||||
|
if ($link->getAttribute('rel') === 'stylesheet') {
|
||||||
|
$linksToRemove[] = $link;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach ($linksToRemove as $link) {
|
||||||
|
$link->parentNode->removeChild($link);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Charger et résoudre le CSS
|
||||||
|
$fullCssPath = kirby()->root() . '/' . $cssPath;
|
||||||
|
$resolvedCss = resolveCssImports($fullCssPath);
|
||||||
|
|
||||||
|
// Ajouter les règles @page pour le format d'impression
|
||||||
|
$pageRules = "\n\n@page {\n size: " . $printFormat . ";\n}\n";
|
||||||
|
$resolvedCss .= $pageRules;
|
||||||
|
|
||||||
|
// Créer une balise <style> et l'ajouter au <head>
|
||||||
|
$styleTag = $dom->createElement('style');
|
||||||
|
$styleTag->appendChild($dom->createTextNode($resolvedCss));
|
||||||
|
$head->appendChild($styleTag);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remplacer les URLs localhost par les URLs de production
|
||||||
|
$html = $dom->saveHTML();
|
||||||
|
$siteUrl = kirby()->site()->url();
|
||||||
|
$html = str_replace('http://localhost:8888', $siteUrl, $html);
|
||||||
|
|
||||||
|
$data = json_encode(['html' => $html]);
|
||||||
|
|
||||||
|
$apiKey = kirby()->option('web2printApiKey');
|
||||||
|
|
||||||
|
curl_setopt_array($ch, [
|
||||||
|
CURLOPT_RETURNTRANSFER => true,
|
||||||
|
CURLOPT_POST => true,
|
||||||
|
CURLOPT_HTTPHEADER => [
|
||||||
|
'Content-Type: application/json',
|
||||||
|
'X-API-Key: ' . $apiKey
|
||||||
|
],
|
||||||
|
CURLOPT_POSTFIELDS => $data
|
||||||
|
]);
|
||||||
|
|
||||||
|
$response = curl_exec($ch);
|
||||||
|
|
||||||
|
if ($response === false) {
|
||||||
|
$error = curl_error($ch);
|
||||||
|
curl_close($ch);
|
||||||
|
http_response_code(500);
|
||||||
|
return json_encode(['error' => 'cURL error: ' . $error]);
|
||||||
|
}
|
||||||
|
|
||||||
|
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||||
|
curl_close($ch);
|
||||||
|
|
||||||
|
if ($httpCode === 200) {
|
||||||
|
try {
|
||||||
|
// Générer un nom de fichier unique avec timestamp et format
|
||||||
|
$timestamp = date('Y-m-d_H-i-s');
|
||||||
|
$filename = "export_{$printFormat}_{$timestamp}.pdf";
|
||||||
|
|
||||||
|
// Créer un fichier temporaire
|
||||||
|
$tmpFile = tempnam(sys_get_temp_dir(), 'pdf_');
|
||||||
|
file_put_contents($tmpFile, $response);
|
||||||
|
|
||||||
|
// Créer le fichier dans Kirby
|
||||||
|
$file = $page->createFile([
|
||||||
|
'source' => $tmpFile,
|
||||||
|
'filename' => $filename
|
||||||
|
]);
|
||||||
|
|
||||||
|
// Supprimer le fichier temporaire
|
||||||
|
unlink($tmpFile);
|
||||||
|
|
||||||
|
return json_encode([
|
||||||
|
'success' => true,
|
||||||
|
'message' => 'PDF généré avec succès',
|
||||||
|
'filename' => $filename
|
||||||
|
]);
|
||||||
|
} catch (\Exception $e) {
|
||||||
|
http_response_code(500);
|
||||||
|
return json_encode([
|
||||||
|
'error' => 'Failed to save PDF: ' . $e->getMessage()
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
http_response_code($httpCode);
|
||||||
|
$error = json_decode($response, true);
|
||||||
|
return json_encode($error ?: [
|
||||||
|
'error' => 'Unknown error',
|
||||||
|
'httpCode' => $httpCode,
|
||||||
|
'response' => $response
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
];
|
||||||
Loading…
Add table
Add a link
Reference in a new issue