第 10 章 統合ガイドとコードサンプル
第 10 章 統合ガイドとコードサンプル
10.1 統合パターンの概観
| 目的 | 方法 |
|---|---|
| Web ページに印刷ボタン | /sdk/wxprint.js を読み込み → wp.print({template, data, printer}) |
| Web のプレビュー後印刷 | wp.preview({...}) → トレイのウィンドウ(または /preview/{job_id}) |
| バックエンドでのドキュメント出力 | POST /api/print(テンプレート id + インラインデータまたはデータ URL)+ ポーリング |
| 印刷時のデータ取得 | data: {url, headers} — サーバーが取得 |
| 動的なレイアウト組み立て | POST /api/script の動詞シーケンス(または SDK の wp.document()) |
| バッチ出荷 | data に配列を渡す + copies_per_record |
| LAN 印刷サーバー | bind: "0.0.0.0" + トークン。全マシンがトークン付きで送信 |
| アーカイブ | POST /api/saveas(pdf/png/zip)またはプレビューウィンドウからのエクスポート |
リポジトリの examples/ ディレクトリに、すべてのサンプルの実行可能なバージョンがあります(本章のコードはそこから採録しています)。
examples/
├── websocket/ Browser demos (1 pure HTML / 2 overlay / 3 Twig / 4 feature gallery)
└── http/ Backend samples (python/php/nodejs/java/golang/
rust/c/cpp/csharp/ruby) + mock_api.py (URL data-source mock)
10.2 curl での簡単確認
# Is the service running?
curl -s http://127.0.0.1:8899/api/version
# List printers
curl -s http://127.0.0.1:8899/api/printers
# Submit a print (template id + inline data)
curl -s -X POST http://127.0.0.1:8899/api/print \
-H "Content-Type: application/json" \
-d '{"template": "invoice-a4", "data": {"no": "INV-001"},
"printer": "HP-LaserJet-M1005"}'
# Check status
curl -s http://127.0.0.1:8899/api/jobs/<job_id>
10.3 Python(標準ライブラリ urllib)
import json, time, urllib.request
BASE, TOKEN = "http://127.0.0.1:8899", "" # fill in when the service sets a token
def api(method, path, body=None, raw=False):
req = urllib.request.Request(BASE + path, method=method)
if TOKEN:
req.add_header("Authorization", f"Bearer {TOKEN}")
data = None
if body is not None:
data = json.dumps(body).encode()
req.add_header("Content-Type", "application/json")
with urllib.request.urlopen(req, data=data, timeout=30) as resp:
payload = resp.read()
return payload if raw else json.loads(payload)
# 1) List printers
printers = api("GET", "/api/printers")["printers"]
printer = next((p["name"] for p in printers if p["state"] == "ready"), "")
# 2) Submit: inline overlay template + inline data
template = {
"id": "demo-waybill-py", "name": "Python sample waybill", "kind": "overlay",
"page": {"width_mm": 100, "height_mm": 180},
"elements": [
{"type": "text", "id": "title", "x": 5, "y": 6, "w": 90, "h": 8,
"text": "Shipping label (submitted from Python)",
"font": {"family": "", "size_pt": 12, "weight": 700}},
{"type": "barcode", "id": "bc", "x": 5, "y": 20, "w": 90, "h": 14,
"symbology": "code128", "content": "{{no}}", "show_text": True},
{"type": "text", "id": "addr", "x": 5, "y": 40, "w": 90, "h": 16,
"text": "To: {{name}}\n{{addr}}", "font": {"family": "", "size_pt": 8}},
],
}
job = api("POST", "/api/print", {
"template": template,
"data": {"no": "PY-2026-0001", "name": "Alice",
"addr": "100 Wensan Road, Xihu District, Hangzhou"},
"printer": printer,
})
job_id = job["job_id"]
# 3) URL data form (fetched server-side; headers pass auth through)
job_url = api("POST", "/api/print", {
"template": {"url": "https://erp.example.com/tpl/order-label.json"},
"data": {"url": "https://erp.example.com/api/data/orders", # array = batch
"headers": {"Authorization": "Bearer erp-token"}, "timeout_s": 5},
})
# 4) Poll status
for _ in range(60):
st = api("GET", f"/api/jobs/{job_id}")
if st["status"] in ("done", "failed", "cancelled"):
break
time.sleep(0.5)
# 5) Register the template (inline submissions are not registered)
reg = api("POST", "/api/templates", template)
# 6) Preview PDF (no paper; reference by id)
pdf = api("POST", "/api/preview?format=pdf",
{"template": "demo-waybill-py", "data": {"no": "PY-2026-0002"}},
raw=True)
open("/tmp/wxprint-preview.pdf", "wb").write(pdf)
10.4 Node.js(組み込み fetch、18+)
const BASE = "http://127.0.0.1:8899";
const TOKEN = "";
async function api(method, path, body) {
const headers = { "Content-Type": "application/json" };
if (TOKEN) headers.Authorization = `Bearer ${TOKEN}`;
const resp = await fetch(BASE + path, {
method, headers,
body: body === undefined ? undefined : JSON.stringify(body),
});
if (!resp.ok) throw new Error(`HTTP ${resp.status}: ${await resp.text()}`);
return resp.json();
}
// Batch printing: an array = 3 labels fed continuously
const job = await api("POST", "/api/print", {
template: {
id: "demo-label-node", kind: "overlay",
page: { width_mm: 60, height_mm: 40 },
elements: [
{ type: "text", id: "sku", x: 3, y: 3, w: 54, h: 6,
text: "{{sku}}", font: { family: "", size_pt: 9, weight: 700 } },
{ type: "barcode", id: "bc", x: 3, y: 11, w: 54, h: 12,
symbology: "code128", content: "{{code}}", show_text: false },
],
},
data: [
{ sku: "Wireless mouse (black)", code: "SKU-10001", qty: 24, bin: "A-01-03" },
{ sku: "Wireless mouse (white)", code: "SKU-10002", qty: 18, bin: "A-01-04" },
{ sku: "Wired keyboard 87-key", code: "SKU-10003", qty: 12, bin: "B-02-01" },
],
printer: "",
});
for (let i = 0; i < 60; i++) {
const st = await api("GET", `/api/jobs/${job.job_id}`);
if (["done", "failed", "cancelled"].includes(st.status)) break;
await new Promise(r => setTimeout(r, 500));
}
10.5 Java(java.net.http.HttpClient、JDK 11+)
import java.net.URI;
import java.net.http.*;
import java.time.Duration;
public class PrintWaybill {
static final String BASE = "http://127.0.0.1:8899";
static final String TOKEN = "";
static final HttpClient HTTP = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5)).build();
static String api(String method, String path, String json) throws Exception {
HttpRequest.Builder req = HttpRequest.newBuilder(URI.create(BASE + path))
.timeout(Duration.ofSeconds(30))
.header("Content-Type", "application/json");
if (!TOKEN.isEmpty()) req.header("Authorization", "Bearer " + TOKEN);
req.method(json == null ? "GET" : method,
json == null ? HttpRequest.BodyPublishers.noBody()
: HttpRequest.BodyPublishers.ofString(json));
HttpResponse<String> resp = HTTP.send(req.build(),
HttpResponse.BodyHandlers.ofString());
if (resp.statusCode() >= 400)
throw new RuntimeException("HTTP " + resp.statusCode() + ": " + resp.body());
return resp.body();
}
public static void main(String[] args) throws Exception {
// Submit: inline overlay template (prefer Jackson/Gson POJOs in production)
String body = """
{ "template": {
"id": "demo-waybill-java", "kind": "overlay",
"page": { "width_mm": 100, "height_mm": 100 },
"elements": [
{ "type": "text", "id": "title", "x": 5, "y": 5, "w": 90, "h": 8,
"text": "Express waybill (submitted from Java)",
"font": { "family": "", "size_pt": 12, "weight": 700 } },
{ "type": "barcode", "id": "bc", "x": 5, "y": 18, "w": 90, "h": 14,
"symbology": "code128", "content": "{{no}}", "show_text": true },
{ "type": "qrcode", "id": "qr", "x": 62, "y": 38, "w": 32, "h": 32,
"content": "{{url}}" } ] },
"data": { "no": "JAVA-2026-0001", "url": "https://example.com/t/1" },
"printer": "" }
""";
System.out.println(api("POST", "/api/print", body));
}
}
10.6 Go(標準ライブラリ net/http)
func api(method, path string, body any) (string, error) {
var rd io.Reader
if body != nil {
b, _ := json.Marshal(body)
rd = bytes.NewReader(b)
}
req, _ := http.NewRequest(method, "http://127.0.0.1:8899"+path, rd)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil { return "", err }
defer resp.Body.Close()
b, _ := io.ReadAll(resp.Body)
if resp.StatusCode >= 400 {
return "", fmt.Errorf("HTTP %d: %s", resp.StatusCode, b)
}
return string(b), nil
}
// Inline html+twig template (entry = a Twig string rendered server-side)
job, err := api("POST", "/api/print", map[string]any{
"template": map[string]any{
"id": "demo-report-go", "kind": "html",
"entry": `<h2>Inventory daily report ({{date}})</h2>
<table border="1" cellpadding="4" style="border-collapse:collapse;width:100%">
<tr style="background:#eee"><th>Warehouse</th><th>SKUs</th></tr>
{% for w in warehouses %}<tr><td>{{w.name}}</td><td>{{w.sku_count}}</td></tr>{% endfor %}
</table>`,
"page": map[string]any{"width_mm": 210, "height_mm": 297,
"margin_mm": map[string]float64{"top": 15, "bottom": 15, "left": 14, "right": 14}},
},
"data": map[string]any{"date": "2026-10-24",
"warehouses": []map[string]any{{"name": "Hangzhou WH1", "sku_count": 1024}}},
})
10.7 PHP(cURL 拡張)
function api(string $method, string $path, ?array $body = null) {
$ch = curl_init("http://127.0.0.1:8899" . $path);
curl_setopt_array($ch, [CURLOPT_RETURNTRANSFER => true,
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_TIMEOUT => 30]);
$headers = ['Content-Type: application/json'];
if (TOKEN !== '') { $headers[] = 'Authorization: Bearer ' . TOKEN; }
if ($body !== null) {
curl_setopt($ch, CURLOPT_POSTFIELDS,
json_encode($body, JSON_UNESCAPED_UNICODE));
}
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$resp = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($code >= 400) { fwrite(STDERR, "HTTP $code: $resp\n"); exit(1); }
return json_decode($resp, true);
}
$job = api('POST', '/api/print', [
'template' => 'invoice-a4',
'data' => ['no' => 'INV-2026-1042', 'amount' => 1280.50],
'printer' => 'HP-LaserJet-M1005',
]);
10.8 C#(.NET 8、NuGet 依存ゼロ)
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
using var http = new HttpClient { BaseAddress = new Uri("http://127.0.0.1:8899") };
var body = new {
template = new {
id = "demo-label-csharp", kind = "overlay",
page = new { width_mm = 60, height_mm = 40 },
elements = new object[] {
new { type = "text", id = "title", x = 3, y = 3, w = 54, h = 6,
text = "{{title}}", font = new { family = "", size_pt = 8, weight = 700 } },
new { type = "barcode", id = "bc", x = 3, y = 11, w = 54, h = 12,
symbology = "code128", content = "{{isbn}}", show_text = false },
},
},
data = new { title = "Programming Rust (2nd ed.)", isbn = "978-7-111-00000-0" },
};
var resp = await http.PostAsJsonAsync("/api/print", body);
resp.EnsureSuccessStatusCode();
var job = await resp.Content.ReadFromJsonAsync<JsonElement>();
Console.WriteLine($"job_id={job.GetProperty("job_id")}");
10.9 Ruby(標準ライブラリ net/http)
require 'net/http'
require 'json'
def api(method, path, body = nil)
uri = URI("http://127.0.0.1:8899#{path}")
req = Net::HTTP.const_get(method.capitalize).new(uri)
req['Content-Type'] = 'application/json'
req.body = JSON.generate(body) if body
Net::HTTP.start(uri.host, uri.port) { |http| http.request(req).body }
end
# 58mm receipt: inline html template (entry = a Twig string)
job = JSON.parse(api('post', '/api/print', {
'template' => {
'id' => 'demo-receipt-ruby', 'kind' => 'html',
'entry' => <<~HTML,
<div style="font-family:monospace">
<h3 style="margin:0">{{shop}}</h3>
{% for r in rows %}<div>{{r.name|escape}} × {{r.qty}} … {{r.price}}</div>{% endfor %}
<div style="text-align:right"><b>Total: {{total}}</b></div>
</div>
HTML
'page' => { 'width_mm' => 58, 'height_mm' => 120,
'margin_mm' => { 'top' => 4, 'bottom' => 4, 'left' => 4, 'right' => 4 } }
},
'data' => { 'shop' => "Sample convenience store",
'rows' => [{ 'name' => 'Coffee', 'qty' => 2, 'price' => 12 }],
'total' => 24 },
}))
10.10 C / C++ / Rust
3 つすべてのサンプルは examples/http/{c,cpp,rust}/ にあります(libcurl / 純粋な標準ライブラリ)。核となる作業は同じです:/api/print に JSON を POST し、job_id をパースし、/api/jobs/{id} をポーリングします。Rust のスケッチ(標準ライブラリのみ):
// Submit → parse job_id (JSON assembly/parsing omitted; see examples/http/rust)
let resp = http_post("http://127.0.0.1:8899/api/print", &body_json)?;
// Poll
loop {
let st = http_get(&format!("http://127.0.0.1:8899/api/jobs/{job_id}"))?;
if st.contains("\"done\"") || st.contains("\"failed\"") { break; }
std::thread::sleep(std::time::Duration::from_millis(500));
}
10.11 Web 統合
方法 1:JS SDK(推奨)
<script src="http://127.0.0.1:8899/sdk/wxprint.js"></script>
<script>
const wp = new wxprint.WxPrint({ token: "T" });
wp.onJob(ev => updateProgress(ev.page_done, ev.total_pages));
document.getElementById("print-btn").onclick = async () => {
// Preview form: the tray pops the native preview window on done;
// the user confirms and it goes to paper
await wp.preview({ template: "invoice-a4",
data: { url: "/api/invoice/2026-1042" } });
// Direct form: submitted and printed immediately (high-volume
// waybill/receipt scenarios)
// await wp.print({ template: "sf-waybill-100x93", data: {...},
// printer: "HP-LaserJet-M1005" });
};
</script>
方法 2:生 WebSocket(5.4 節の例を参照。再接続は自前で実装する必要があります——SDK には内蔵されています)。
ポップアップブロッカーの注意:プレビュー確認ページで window.open を使う場合、ユーザージェスチャー(クリック)内で同期的に呼び出す必要があります。非同期コールバック内で開くとブロックされます。about:blank を同期的に開いて「生成中…」のプレースホルダーを書き、完了時に win.location.href を /preview/{job_id} に設定してください。
10.12 適応的な接続規約
サンプルとデモページは 1 つの規約に従っています。業務統合でも採用を推奨します。
- 環境変数
WXPRINT_BASE/WXPRINT_TOKEN(明示的)。 ~/.wxprint/config.json(トレイが設定した bind/token に自動適応。0.0.0.0は127.0.0.1に解決)。- デフォルト
http://127.0.0.1:8899匿名。
ブラウザーデモはさらに、URL パラメーター ?host=&token= を明示的な上書きとして受け付けます。
10.13 Lodop からの移行
自動移行ツールはありません。完全な動詞対応マトリクスが docs/lodop-parity.md に同梱されています。
| Lodop | WxPrint |
|---|---|
PRINT_INIT / PRINT_INITA |
init + pagesize |
ADD_PRINT_TEXT/HTM/TABLE/BARCODE/QRCODE/IMAGE/RECT/LINE |
add_text / add_html / add_table / add_barcode / add_qrcode / add_image / add_rect / add_line |
SET_PRINT_PAGESIZE(0.1mm 単位) |
pagesize(mm 単位。座標はそのまま変換) |
SET_PRINT_STYLE(A) |
set_style / set_stylea |
NEWPAGE |
newpage |
| ItemType 1/2/3/4(ヘッダー/フッター/先頭/末尾) | 要素の scope |
| PageNumber | {{page}} / {{pages}} |
PRINT / PRINTA / PRINT_PREVIEW / SAVEAS |
print / プレビューでのプリンター選択 / preview / saveas(PRINT_SETUP の要素レベルの微調整は今後の計画項目) |
ADD_PRINT_SETUP_BKIMG + SET_SHOW_MODE "BKIMG_PRINT" |
テンプレートの background.image + background.print |
座標は 0.1mm から mm へそのまま変換できます。PRINT_SETUP で行ったフィールドの微調整は独自形式のため、新しいシステムでやり直す必要があります(オーバーライドファイルの方がむしろバックアップしやすいです)。