// controllers/print_frame_controller.js import { Controller } from '@hotwired/stimulus'; /** * 특정 주소를 iframe으로 불러와서 인쇄하는 Stimulus 컨트롤러 */ export default class extends Controller { static values = { url: String, }; connect() { // 재사용할 iframe이 없으면 생성 if (!this.iframe) { this.iframe = document.createElement('iframe'); this.iframe.style.display = 'none'; this.iframe.onload = this.handleLoad.bind(this); document.body.appendChild(this.iframe); } } disconnect() { if (this.iframe) { this.iframe.remove(); this.iframe = null; } } print() { if (!this.urlValue) { console.warn('URL 값이 없습니다.'); return; } this._shouldPrint = true; this.iframe.src = this.urlValue; } handleLoad() { if (this._shouldPrint) { try { this.iframe.contentWindow.focus(); this.iframe.contentWindow.print(); } catch (e) { console.error('Print 실패:', e); } this._shouldPrint = false; } } }