[ 'label' => 'A4', 'dmPaperSize' => 9, 'pageSize' => 'A4 portrait', 'pageMargin' => '10mm', 'contentWidth' => '190mm', 'cardWidth' => '140mm', 'cardHeight' => '200mm', ], 'a5' => [ 'label' => 'A5', 'dmPaperSize' => 11, 'pageSize' => 'A5 portrait', 'pageMargin' => '8mm', 'contentWidth' => '128mm', 'cardWidth' => '100mm', 'cardHeight' => '148mm', ], 'b5' => [ 'label' => 'B5', 'dmPaperSize' => 13, 'pageSize' => 'B5 portrait', 'pageMargin' => '8mm', 'contentWidth' => '176mm', 'cardWidth' => '120mm', 'cardHeight' => '170mm', ], 'letter' => [ 'label' => 'Letter', 'dmPaperSize' => 1, 'pageSize' => 'letter portrait', 'pageMargin' => '10mm', 'contentWidth' => '190mm', 'cardWidth' => '140mm', 'cardHeight' => '200mm', ], // 贺卡常用竖版 10×15cm 'card_10x15' => [ 'label' => '贺卡10×15cm', 'dmPaperSize' => 0, 'pageSize' => '100mm 150mm', 'pageMargin' => '5mm', 'contentWidth' => '90mm', 'cardWidth' => '90mm', 'cardHeight' => '130mm', ], // 贺卡竖版 13×18cm 'card_13x18' => [ 'label' => '贺卡13×18cm', 'dmPaperSize' => 0, 'pageSize' => '130mm 180mm', 'pageMargin' => '6mm', 'contentWidth' => '118mm', 'cardWidth' => '118mm', 'cardHeight' => '160mm', ], ]; } /** * 解析纸张规格 * * @param array $options scene, paperType, paperSize(dmPaperSize 数值优先) */ public static function resolve(array $options = []) { $scene = (string)($options['scene'] ?? self::SCENE_DEFAULT); $paperType = strtolower(trim((string)($options['paperType'] ?? ''))); $paperSize = intval($options['paperSize'] ?? 0); if ($paperType === '') { $paperType = strtolower(trim((string)self::envByScene($scene, 'PAPER_TYPE'))); } if ($paperSize <= 0) { $paperSize = intval(self::envByScene($scene, 'PAPER_SIZE')); } if ($paperType === '') { $paperType = 'a4'; } $presets = self::presets(); if (!isset($presets[$paperType])) { throw new CloudPrinterApiException('不支持的纸张类型: ' . $paperType); } $spec = $presets[$paperType]; $dmPaperSize = $paperSize > 0 ? $paperSize : intval($spec['dmPaperSize']); if ($dmPaperSize <= 0) { $dmPaperSize = intval(getenv('LIANKENET_PAPER_SIZE') ?: 9); } return [ 'paperType' => $paperType, 'label' => $spec['label'], 'dmPaperSize' => $dmPaperSize, 'pageSize' => $spec['pageSize'], 'pageMargin' => $spec['pageMargin'], 'contentWidth' => $spec['contentWidth'], 'cardWidth' => $spec['cardWidth'], 'cardHeight' => $spec['cardHeight'], ]; } /** * 返回可选纸张类型列表(给前端或配置参考) */ public static function listTypes() { $list = []; foreach (self::presets() as $key => $item) { $list[] = [ 'paperType' => $key, 'label' => $item['label'], 'defaultDmPaperSize' => $item['dmPaperSize'], ]; } return $list; } /** * 按场景读取 .env:LIANKENET_DELIVERY_SLIP_PAPER_TYPE 等 */ protected static function envByScene($scene, $suffix) { $map = [ self::SCENE_DELIVERY_SLIP => 'LIANKENET_DELIVERY_SLIP_' . $suffix, self::SCENE_GREETING_CARD => 'LIANKENET_GREETING_CARD_' . $suffix, self::SCENE_DEFAULT => 'LIANKENET_' . $suffix, ]; $key = $map[$scene] ?? $map[self::SCENE_DEFAULT]; $value = getenv($key); if ($value === false || $value === '') { if ($scene !== self::SCENE_DEFAULT && $suffix === 'PAPER_TYPE') { return getenv('LIANKENET_PAPER_TYPE') ?: ''; } if ($scene !== self::SCENE_DEFAULT && $suffix === 'PAPER_SIZE') { return getenv('LIANKENET_PAPER_SIZE') ?: 0; } } return $value; } }