admin管理员组文章数量:1405361
I'm trying to render Arabic text using Rust and PrintPDF crate. The text is not shown correctly.
Here is my code. Anyone has any idea how to solve this?
extern crate image as image_official;
extern crate printpdf;
use clap::Parser;
use printpdf::*;
use std::path::Path;
use structs::PageSize;
use std::fs::{self, File};
use std::io::{BufWriter, Write};
pub mod structs;
pub mod utils;
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
#[arg(short, long)]
name: String,
#[arg(short, long)]
amount: f64,
#[arg(short, long)]
date: String,
#[arg(short, long)]
logo: String,
#[arg(short, long)]
output_path: String,
}
static NOTO_SANS_TTF: &[u8] =
include_bytes!("../fonts/noto_sans/NotoSans-VariableFont_wdth,wght.ttf");
static NOTO_AR_TTF: &[u8] =
include_bytes!("../fonts/Noto_Sans_Arabic/NotoSansArabic-VariableFont_wdth,wght.ttf");
fn generate_receipt(_name: &str, _amount: f64, _date: &str, _logo: &str, output_path: &str) {
let page_size = PageSize::<f32>::a4();
let path = Path::new(output_path);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent).expect("Failed to create directory");
}
let mut doc = PdfDocument::new("Receipt");
let font_noto = ParsedFont::from_bytes(NOTO_SANS_TTF, 0, &mut Vec::new()).unwrap();
let _font_noto_id = doc.add_font(&font_noto);
let font_ar = ParsedFont::from_bytes(NOTO_AR_TTF, 0, &mut Vec::new()).unwrap();
let font_ar_id = doc.add_font(&font_ar);
let ops = vec![
Op::SaveGraphicsState,
Op::StartTextSection,
Op::SetTextCursor {
pos: Point::new(Mm(20.0), Mm(270.0)),
},
Op::SetFontSizeBuiltinFont {
size: Pt(24.0),
font: BuiltinFont::Helvetica,
},
Op::SetLineHeight { lh: Pt(24.0) },
Op::SetFillColor {
col: Color::Rgb(Rgb {
r: 0.0,
g: 0.0,
b: 0.8,
icc_profile: None,
}),
},
Op::WriteTextBuiltinFont {
items: vec![TextItem::Text("Hello from Helvetica!".to_string())],
font: BuiltinFont::Helvetica,
},
Op::AddLineBreak,
Op::SetFontSizeBuiltinFont {
size: Pt(18.0),
font: BuiltinFont::TimesRoman,
},
Op::SetLineHeight { lh: Pt(18.0) },
Op::SetFillColor {
col: Color::Rgb(Rgb {
r: 0.8,
g: 0.0,
b: 0.0,
icc_profile: None,
}),
},
Op::WriteTextBuiltinFont {
items: vec![TextItem::Text("This is Times Roman font".to_string())],
font: BuiltinFont::TimesRoman,
},
Op::AddLineBreak,
Op::SetFontSize {
size: Pt(14.0),
font: font_ar_id.clone(),
},
Op::SetLineHeight { lh: Pt(14.0) },
Op::SetFillColor {
col: Color::Rgb(Rgb {
r: 0.0,
g: 0.6,
b: 0.0,
icc_profile: None,
}),
},
Op::SetTextRenderingMode {
mode: TextRenderingMode::Fill,
},
Op::WriteText {
items: vec![TextItem::Text("السلام عليكم".to_string())], // Corrected Arabic text
font: font_ar_id.clone(),
},
Op::EndTextSection,
Op::RestoreGraphicsState,
];
let page1 = PdfPage::new(Mm(page_size.width), Mm(page_size.height), ops);
let pdf_bytes: Vec<u8> = doc
.with_pages(vec![page1])
.save(&PdfSaveOptions::default(), &mut Vec::new());
let file = File::create(output_path).expect("Failed to create file");
let mut writer = BufWriter::new(file);
writer
.write_all(&pdf_bytes)
.expect("Failed to write PDF data");
println!("Created text_example.pdf");
}
fn main() {
let args = Args::parse();
generate_receipt(
&args.name,
args.amount,
&args.date,
&args.logo,
&args.output_path,
);
println!("Receipt generated at {}", args.output_path);
}
rustc 1.85.0 (4d91de4e4 2025-02-17)
Cargo.toml:
[dependencies]
allsorts = "0.15.1"
clap = { version = "4.5.32", features = ["derive"] }
fontdue = "0.9.3"
harfbuzz_rs = "2.0.1"
image = "0.24.9"
printpdf = { version = "0.8.2", features = ["png", "jpeg"] }
rustybuzz = "0.20.1"
unicode-bidi = "0.3.18"
Output:
Correct Output must be: السلام عليكم
Thanks!
本文标签: pdfRender Arabic text using Rust and PrintpdfStack Overflow
版权声明:本文标题:pdf - Render Arabic text using Rust and Printpdf - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744226219a2596102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论