// eBook AI Writer - Version Web avec API OpenAI et Export PDF import React, { useState, useRef } from "react"; import { Textarea } from "@/components/ui/textarea"; import { Button } from "@/components/ui/button"; import html2pdf from "html2pdf.js"; export default function EbookAIWriter() { const [topic, setTopic] = useState(""); const [generatedBook, setGeneratedBook] = useState(""); const [loading, setLoading] = useState(false); const contentRef = useRef(null); const generateEbook = async () => { if (!topic.trim()) return; setLoading(true); try { const response = await fetch("https://api.openai.com/v1/chat/completions", { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer YOUR_OPENAI_API_KEY`, }, body: JSON.stringify({ model: "gpt-4", messages: [ { role: "system", content: "Tu es un écrivain d'eBooks professionnel. Structure un petit livre clair avec chapitres sur un sujet donné.", }, { role: "user", content: `Écris un eBook structuré sur le sujet suivant : ${topic}`, }, ], temperature: 0.7, max_tokens: 1500, }), }); const data = await response.json(); const result = data.choices?.[0]?.message?.content || "Aucune réponse générée."; setGeneratedBook(result); } catch (error) { setGeneratedBook("Erreur lors de la génération de l'eBook. Vérifie ta clé API."); console.error("Erreur API:", error); } finally { setLoading(false); } }; const downloadPDF = () => { if (contentRef.current) { const element = contentRef.current; const opt = { margin: 0.5, filename: `${topic || "ebook"}.pdf`, image: { type: "jpeg", quality: 0.98 }, html2canvas: { scale: 2 }, jsPDF: { unit: "in", format: "letter", orientation: "portrait" }, }; html2pdf().set(opt).from(element).save(); } }; return (
🧠 eBook AI Writer
Entrez un sujet et l'IA vous génère un eBook de base.
Commentaires
Enregistrer un commentaire
Partage des pub