| 543 | } |
| 544 | |
| 545 | void Pdf::saveToFile( const std::filesystem::path& documentPath ) |
| 546 | { |
| 547 | if ( documentPath.empty() ) |
| 548 | { |
| 549 | spdlog::error( "Pdf: Error: empty path." ); |
| 550 | return; |
| 551 | } |
| 552 | |
| 553 | if ( !state_->document ) |
| 554 | { |
| 555 | spdlog::warn( "Pdf: Can't save to file: no valid document." ); |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | // reset all errors before saving document |
| 560 | HPDF_ResetError( state_->document ); |
| 561 | |
| 562 | |
| 563 | /* save the document to a stream */ |
| 564 | MR_HPDF_CHECK_RES_STATUS( HPDF_SaveToStream( state_->document ) ); |
| 565 | |
| 566 | /* rewind the stream. */ |
| 567 | HPDF_ResetStream( state_->document ); |
| 568 | |
| 569 | auto pathString = utf8string( documentPath ); |
| 570 | std::ofstream outFile( documentPath, std::ios::binary ); |
| 571 | if ( !outFile ) |
| 572 | { |
| 573 | spdlog::error( "Pdf: Error while saving pdf to file \"{}\"", pathString ); |
| 574 | return; |
| 575 | } |
| 576 | |
| 577 | HPDF_UINT32 streamSize = HPDF_GetStreamSize( state_->document ); |
| 578 | const HPDF_UINT32 maxBufSize = 4096; |
| 579 | /* get the data from the stream and output it to stdout. */ |
| 580 | while ( streamSize > 0 ) |
| 581 | { |
| 582 | HPDF_BYTE buf[maxBufSize]; |
| 583 | HPDF_UINT32 size = std::min( streamSize, maxBufSize ); |
| 584 | streamSize -= size; |
| 585 | MR_HPDF_CHECK_RES_STATUS( HPDF_ReadFromStream( state_->document, buf, &size ) ); |
| 586 | |
| 587 | if ( !outFile.write( ( const char* )buf, size ) ) |
| 588 | { |
| 589 | spdlog::error( "Pdf: Error while saving pdf to file \"{}\"", pathString ); |
| 590 | break; |
| 591 | } |
| 592 | } |
| 593 | } |
| 594 | |
| 595 | Vector2f Pdf::getPageSize() const |
| 596 | { |