Fix PDF diagram text rendering with format-optimized generation
Problem: SVG diagrams had missing text in PDFs due to Inkscape conversion issues with foreignObject elements used by Mermaid for HTML text rendering. Solution: Implement format-specific optimal generation: - HTML: SVG files for perfect vector scaling and text rendering - PDF: High-resolution PNG files (3x scale) for sharp images with readable text Changes: - Generate SVG for HTML output using standard mmdc command - Generate PNG for PDF output using mmdc with --scale 3 for high resolution - Remove SVG LaTeX package dependency and Inkscape requirement - Update documentation to reflect the dual-format approach Results: - HTML: Crisp, scalable vector diagrams with perfect text - PDF: Sharp, high-resolution raster diagrams with clear text - No external dependencies beyond mermaid-cli 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
+11
-7
@@ -58,19 +58,23 @@ processMermaidBlock (SourceDir sourceDir) (OutputPath outputPath) (CodeBlock (id
|
||||
let DiagramId diagId = generateDiagramId id' contents
|
||||
diagIdStr = T.unpack diagId
|
||||
mmdFile = sourceDir </> diagIdStr <> ".mmd"
|
||||
pngFile = sourceDir </> diagIdStr <> ".png"
|
||||
-- Determine the correct image path based on output format
|
||||
imagePath = if isHTMLOutput outputPath
|
||||
then takeFileName pngFile -- Just filename for HTML
|
||||
else pngFile -- Full path for PDF/LaTeX
|
||||
-- Use SVG for HTML (scalable), high-res PNG for PDF (text compatibility)
|
||||
(outputFile, imagePath) = if isHTMLOutput outputPath
|
||||
then let svgFile = sourceDir </> diagIdStr <> ".svg"
|
||||
in (svgFile, takeFileName svgFile)
|
||||
else let pngFile = sourceDir </> diagIdStr <> ".png"
|
||||
in (pngFile, pngFile)
|
||||
|
||||
-- Use bracket to ensure cleanup of temporary mermaid file
|
||||
bracket
|
||||
(TIO.writeFile mmdFile contents >> return mmdFile)
|
||||
(\file -> removeFile file `catch` \(_ :: SomeException) -> return ())
|
||||
(\_ -> do
|
||||
void $ callProcess mermaidCommand ["-i", mmdFile, "-o", pngFile]
|
||||
putStrLn $ successEmoji <> " Generated " <> pngFile
|
||||
-- Generate with appropriate format and quality for output type
|
||||
if isHTMLOutput outputPath
|
||||
then void $ callProcess mermaidCommand ["-i", mmdFile, "-o", outputFile]
|
||||
else void $ callProcess mermaidCommand ["-i", mmdFile, "-o", outputFile, "--scale", "3"]
|
||||
putStrLn $ successEmoji <> " Generated " <> outputFile
|
||||
return $ Para [Image nullAttr [] (T.pack imagePath, "Mermaid diagram")])
|
||||
processMermaidBlock _ _ block = return block
|
||||
|
||||
|
||||
Reference in New Issue
Block a user