Lỗi file PDF bị block, không xem trước (preview) được thường gặp trên Windows (đặc biệt sau khi tải file từ Internet hoặc email). Bạn làm theo các cách sau:
1️⃣ Gỡ chặn (Unblock) file PDF
Windows sẽ tự động chặn file tải từ Internet.
Cách làm:
-
Chuột phải vào file .pdf
-
Chọn Properties
-
Ở tab General
-
Nếu thấy dòng Security: This file came from another computer…
→ Tick Unblock -
Nhấn Apply → OK
-
Mở lại file
📌 Áp dụng cho:
-
Không preview trong File Explorer
-
Mở PDF bị trống / báo lỗi
Nếu bạn muốn làm nguyên cho một thư mục:
Dùng powershell và đánh lệnh bên dưới: sửa đường dẫn tới file Unblock-PDFs.ps1 và foder cần unlok
powershell -ExecutionPolicy Bypass -File "C:\Users\Desktop PC\Desktop\Unblock-PDFs.ps1" -FolderPath "D:\ABC" -Recurse
File: Unblock-PDFs.ps1 các bạn copy code bên dưới về và đặt tên Unblock-PDFs.ps1
Trong đó:
- - Unblock-PDFs.ps1 tên file powershell
- - D:\ABC là thư mục chứa file PDF cần unblock hàng loạt
[CmdletBinding(SupportsShouldProcess=$true)]
param(
[Parameter(Mandatory=$true)]
[string]$FolderPath,
[Parameter(Mandatory=$false)]
[switch]$Recurse,
[Parameter(Mandatory=$false)]
[string]$LogPath = "$env:TEMP\unblock_pdfs_log.txt"
)
function Write-Log {
param($Message)
$time = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
Add-Content -Path $LogPath -Value "$time`t$Message"
}
# kiểm tra thư mục
if (-not (Test-Path $FolderPath)) {
Write-Error "Không tìm thấy thư mục: $FolderPath"
exit 1
}
# tạo file log nếu chưa có
if (-not (Test-Path $LogPath)) {
New-Item -ItemType File -Path $LogPath -Force | Out-Null
}
Write-Host "===> Bắt đầu Unblock các file PDF trong thư mục: $FolderPath"
Write-Host "Tùy chọn đệ quy: $Recurse"
Write-Host "Log: $LogPath"
Write-Host ""
# lấy danh sách file PDF
$searchParams = @{
Path = $FolderPath
Filter = '*.pdf'
File = $true
}
if ($Recurse) { $searchParams.Add('Recurse', $true) }
$files = Get-ChildItem @searchParams -ErrorAction SilentlyContinue
if ($files.Count -eq 0) {
Write-Host "⚠️ Không tìm thấy file PDF nào trong thư mục."
exit 0
}
$total = $files.Count
$done = 0
$skipped = 0
$errors = 0
foreach ($f in $files) {
try {
$path = $f.FullName
$zone = Get-Item -Path $path -Stream Zone.Identifier -ErrorAction SilentlyContinue
if (-not $zone) {
$skipped++
continue
}
if ($PSCmdlet.ShouldProcess($path, "Unblock PDF file")) {
try {
if (Get-Command -Name Unblock-File -ErrorAction SilentlyContinue) {
Unblock-File -Path $path -ErrorAction Stop
}
else {
Remove-Item -Path $path -Stream Zone.Identifier -ErrorAction Stop
}
Write-Host ("[OK] Đã Unblock: " + $path)
Write-Log ("UNBLOCKED`t" + $path)
$done++
}
catch {
Write-Warning ("[LỖI] Khi Unblock " + $path + ": " + $_)
Write-Log ("ERROR`t" + $path + "`t" + $_.Exception.Message)
$errors++
}
}
}
catch {
Write-Warning ("[LỖI] Không xác định khi xử lý " + $f.FullName + ": " + $_)
Write-Log ("ERROR`t" + $f.FullName + "`t" + $_.Exception.Message)
$errors++
}
}
Write-Host ""
Write-Host ("Kết thúc. Unblocked: {0}, Bỏ qua: {1}, Lỗi: {2}, Tổng: {3}" -f $done, $skipped, $errors, $total)
Write-Log ("SUMMARY`tUnblocked:{0}`tSkipped:{1}`tErrors:{2}`tTotal:{3}" -f $done, $skipped, $errors, $total)
Đăng nhận xét